v3ks

Can anyone explain to me the difference between the following:

Thread.Sleep(100);
Application.DoEvents();

and

Application.DoEvents();
Thread.Sleep(100);

I have seen applications with the first method and some with the second
method. Heck, I wrote some applications that I got the desired results using the first method, and some to the second.

Any kind of explanation will be grealty appretiated.


The reason I am asking because I am writing an application (UI) where I need to wait a certain amount of time before the result is acheived. However, I have UI buttons that a user can click - and currently, when they click, the action doesn't take place until the GUI thread is done "sleeping". I tried:

for (..) // loop 8 times
{
Thread.Sleep(50);
Application.DoEvents();
}

But I didn't see that much improvement.

Can anyone suggest something to do in order to keep my application running smoothly and at the same time, allow it to wait for a certain number of msec


Re: Visual C# General Thread.Sleep() & Application.DoEvents()

sirjis

Practically, I can't think of any reason to prefer one over the other. I would choose neither. It is generally better practice to use a separate thread (say using a BackgroundWorker) for long-running background tasks instead of calling Application.DoEvents.

As for your actual question, there is a difference if the thread that it's running on is the message loop thread. If Sleep is called first, then there will be a 100 millisecond (0.1 second) delay in processing messages (fairly short, but noticeable if you are dragging the form at the time). If Sleep is called after, there will still be a delay, but if there was a long running process already before -- long enough to build up messages -- it will wait an additional 100ms before processing. Try changing the 100 to a 5000 and see if you can tell any difference.

But if the thread where this code runs is not the message loop thread, then the message loop thread will still process any messages while this thread sleeps, so Application.DoEvents is unnecessary, and they will act equivalent.

Edit: Hmm, did you edit your question Or did I just not see your question for the right way to do this I would do this by launching another thread in a BackgroundWorker and do the waiting in that thread. Try searching for BackgroundWorker for numerous examples.





Re: Visual C# General Thread.Sleep() & Application.DoEvents()

v3ks

Thanks! I actually was playing with the application, and I did the following:

for (..) // loop 40 times
{
Thread.Sleep(10);
Application.DoEvents();
}



which gave me a much better response... HOWEVER - every once in a while, the application gets stuck - when I pause the application, it is in this loop. I think when Application.DoEvents() executes, sometimes it seems to take all the time it needs. Does this make sense

One more thing - the reason that I am still heading forward with what I am asking about, and not trying to use a backGroundWorker or a different thread is this application is huge, and this was a bug reported regarding the unsmooth response to clicking when it is in a certain mode. Hence, changing the whole structure at this point might not be the best idea, as well as all the risk involved in conducting such changes and extra time it requires!


Thanks though for your answer... at least I know that either way is OK.


v3ks






Re: Visual C# General Thread.Sleep() & Application.DoEvents()

JohnWein

Neither one is any good. Use a timer.