We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

FAQ 14.1 How to display a status dialog in a background thread...

Thread ID:

Created:

Updated:

Platform:

Replies:

1067 Sep 19,2002 08:55 AM Sep 19,2002 04:39 PM Windows Forms 4
loading
Tags: FAQ
Jon Anderson
Asked On September 19, 2002 08:55 AM

I downloaded the sample for this item and when running the main window and the dialog are clearly running on their own threads, however, the main window doesn't seem to be reacting to any paint requests. To repro run the project and move the dialog over the main window and the main window doesn't repaint itself. Why is this? I would like to use this technique but the ugliness of the main window not repainting itself would be too prohibitive. Thanks for any help.

Clay Burch [Syncfusion]
Replied On September 19, 2002 03:59 PM

In that sample, the time consuming operation is just being simulated by an empty while loop. while (System.Environment.TickCount - ticks < 10) ; //empty loop This loop does not give the application a chance to handle pending window messages. If you want window messages processed during your long operation, you should call the Application.DoEvents() method during your processing loop. So, in our sample, if you add this call to the while loop, you will see the messages being processed. while (System.Environment.TickCount - ticks < 10) Application.DoEvents();

Jon Anderson
Replied On September 19, 2002 04:08 PM

Clay, thanks for your reply. I noticed the sample used a continuously executing while loop. I know that will eat the CPU to the exclusion of other messages being processed so I switched the sample to sleep (System.Threading.Thread.Sleep) instead. My understanding is that Sleep frees the cpu to process other events as well. Am I going down the wrong path? What does DoEvents really do? > In that sample, the time consuming operation is just being simulated by an empty while loop. > > while (System.Environment.TickCount - ticks < 10) > ; //empty loop > > > This loop does not give the application a chance to handle pending window messages. If you want window messages processed during your long operation, you should call the Application.DoEvents() method during your processing loop. So, in our sample, if you add this call to the while loop, you will see the messages being processed. > > while (System.Environment.TickCount - ticks < 10) > Application.DoEvents(); >

Jon Anderson
Replied On September 19, 2002 04:25 PM

Further information... I was able to get the underlying window to repaint by placing a form1.Refresh() call in the loop. > Clay, thanks for your reply. > > I noticed the sample used a continuously executing while loop. I know that will eat the CPU to the exclusion of other messages being processed so I switched the sample to sleep (System.Threading.Thread.Sleep) instead. My understanding is that Sleep frees the cpu to process other events as well. Am I going down the wrong path? What does DoEvents really do? > > > In that sample, the time consuming operation is just being simulated by an empty while loop. > > > > while (System.Environment.TickCount - ticks < 10) > > ; //empty loop > > > > > > This loop does not give the application a chance to handle pending window messages. If you want window messages processed during your long operation, you should call the Application.DoEvents() method during your processing loop. So, in our sample, if you add this call to the while loop, you will see the messages being processed. > > > > while (System.Environment.TickCount - ticks < 10) > > Application.DoEvents(); > >

Clay Burch [Syncfusion]
Replied On September 19, 2002 04:39 PM

I don't think Thread.Sleep will allow any of the current thread's window messages be processed as Thread.Sleep just suspends the thread processing for the requested period. Application.DoEvents will allow window messages to be processed. So, as far as our sample, I thnk DoEvents is a better choice. But in your application, the question is whether you want to suspend your thread's processing (with no load on the CPU during the wait), or whether you want your messages to be handled duing the processing loop. There is no way to control how long the processing of messages will take, and that might be a consideration as well.

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;