Question for the MS guys (and gals) :)

I've always wondered about this--

Why is the DoEvents() function in the System.Windows.Forms.Application namespace?

I only ask because I do a lot of console apps for testing, and I always have to add the System.Windows.Forms dll to the project so I can get the DoEvents() functionality. Not using it in my long-running loops causes great system slow-down, so I definitely need it.

Thanks.


UPDATE

Joe, in my comments section, writes:

“DoEvents should not be used in a console app. If you have a CPU-bound console app like the example you gave, you can improve responsiveness of other apps by calling System.Threading.Thread.Sleep() in your loop. A short timeout is probably enough: just to relinquish your timeslice and let other processes in.”

So, I checked out the System.Threading.Thread.Sleep() function and the intellisense gives this little blurb about it:

Specify Zero to indicate that this thread should be suspened to allow other waiting threads to execute.”

As Joe theorized, DoEvents() is probably just a wrapper around System.Threading.Thread.Sleep().

Mystery solved. Thanks to all that replied.

Comments

# re: Question for the MS guys (and gals) :)

Friday, June 11, 2004 10:31 AM by Duncan Jones

I don't think you should use it at all - if you have a long running slow process you should run it on it's own thread.

# re: Question for the MS guys (and gals) :)

Friday, June 11, 2004 10:43 AM by Patrick Steele

DoEvents simply allows a chance for the windows message pump to process messages that may be coming in during a long-running loop.

It's not really a good idea to use it (it can cause re-entrancy issues) and it's pretty much unnecessary since .NET supports multi-threading.

Since console apps don't have a windows message pump running, DoEvents is useless in that environment.

# re: Question for the MS guys (and gals) :)

Friday, June 11, 2004 10:51 AM by Chris McKenzie

I do get a behavior difference if I use it in a Console environment. I have a console app that chuncks through a 30,000+ record file and prints the formatted contents to the console output window. If I run the app without DoEvents(), my other applications slow down. Redrawing takes forever. If I add DoEvents() to the read loop, then I get better behavior out of my other windows.

I'm not entirely sure how multi-threading the loop would change the original behavior of causing long redraw times on my other apps.

What am I missing?

Thanks

# re: Question for the MS guys (and gals) :)

Friday, June 11, 2004 11:56 AM by Joe

As Patrick Steele said, DoEvents should not be used in a console app. If you have a CPU-bound console app like the example you gave, you can improve responsiveness of other apps by calling System.Threading.Thread.Sleep() in your loop. A short timeout is probably enough: just to relinquish your timeslice and let other processes in.

Maybe DoEvents internally calls Thread.Sleep(0) which would explain why it makes a difference, but it's inappropriate in a console app.

# re: Question for the MS guys (and gals) :)

Friday, June 11, 2004 1:13 PM by Chris McKenzie

You know--I'm betting that DoEvents() does call Thread.Sleep() internally. Thanks Joe!

# search for images and videos

Tuesday, April 22, 2008 7:17 AM by search for images and videos

there are many forgotten search engines

Leave a Comment

(required) 
(required) 
(optional)
(required)