tags:

views:

129

answers:

5

I often use this code in winforms applications to wait for events, without using threads.

while(checkSomething()){
    Application.DoEvents();
}

The program and pc is still responsive while the loop run, however if I look cpu usage in task managart, it usage is reported as 100% . Do you know why this happens?

+1  A: 

because... whatever it is it does, it does non-stop?

Nico
I didn't agree: as I know DoEvents just tell Windows to elaborate the message queue. In fact the pc is still responsive. It's someway different from doing while(checkSomething()){}
Andrea Parodi
@Andrea: Nico is correct. Your program is executing `DoEvents()` as fast as it can, over and over. Your other programs are still responsive, but it's still wasteful of processor cycles, and if you're on a laptop, then it's burning battery life.
Michael Petrotta
@Michael: thank you. I think I understand the point: my DoEvents loop is using all cpu cycles whenever there is no messages in queue to elaborate. The program is responsive because messages elaborates just as they come, but I'm wasting CPU
Andrea Parodi
+10  A: 

Application.DoEvents() checks if there are some events that needs to be processed and then process them, and it returns. If your checkSomething() doesn't block on anything you have implemented a busy loop.

That is, your processor is busy running Application.DoEvents() and checkSomething() as fast as it can.

If your checkSomething is not checking data related to other threads, what are you actually checking for ? - sounds like there's rather an event you should be handling instead of polling.

nos
+1  A: 

Because you're doing what's referred to as "busy waiting" if your checkSomething() call returns true all the time. The CPU is running your checkSomething() call. While it evaluates to true, you will remain in the loop, process events, and do it all over again.

Anna Lear
+6  A: 

When you add this:

while(checkSomething()){
    Application.DoEvents();
}

You're basically saying to have the program process windows messages, as fast as possible, without anything stopping it until "checkSomething" returns true.

There are a lot of reasons to avoid DoEvents - that being said, if you really plan to do this, you should try to give up some processing time in your loop:

while(checkSomething()){
    Application.DoEvents();
    Thread.Sleep(10); // Sleep a tiny amount... 
}
Reed Copsey
@Reed: could you please explain which are the reason to avoid it? The program run smoothly and so other programs runnin on the system. It appear to me that the only problem is the cpu percentage reported by task manager.
Andrea Parodi
Having a program use up as much CPU as possible is wasteful when it's not needed, You'll see a slowdown if the PC start doing more concurrent work. (And you'll have fans start spinning on laptops as they'll eventually get hotter with a constant 100% CPU usage)
nos
@Andrea: see this MSDN article, [Keeping your UI Responsive and the Dangers of Application.DoEvents](http://blogs.msdn.com/b/jfoscoding/archive/2005/08/06/448560.aspx), for more reasons to avoid `DoEvents`. You don't see a problem today, but you will six months from now.
Michael Petrotta
@Andrea: Basically, calling DoEvents() passes control to other parts of the program, and can do any number of things - most of which you won't be expecting. This is almost always better accomplished by pushing work into a background thread instead. My blog has a lot of details on threading that may help you. ;)
Reed Copsey
+1  A: 

Application.DoEvents is very dangerous. I used to call it to make the UI of my WinForms application responsive, however you can use BackgroundWorker to make the UI responsive with great ease and you will avoid InvokeRequired overhead.

sh_kamalh