yielding cpu

G

Guest

I am writing a process that is a tight loop that consumes alot of cpu. Back in my VB days I could use "DoEvents" to periodically yield control to the OS incase there were any pending events. This would keep the app from being able to effectively take over the system. What is the equivilant in C#?
 
M

Mattias Sjögren

You can use System.Threading.Thread.Sleep(0) to yield. If you actually
want to pump messages (which is what VB's DoEvents does), you use
System.Windows.Forms.Application.DoEvents().



Mattias
 
N

Niall

Mattias is right. Application.DoEvents() does not block the process, it
pushes the messages on the message loop through, so that it appears to the
user that the gui is still responsive. But the other processes on the
machine will still be CPU starved, if this is what you are trying to avoid.

You may want to investigate using another thread instead of using
Application.DoEvents(). Also, be aware of what App.DoEvents really does. It
stops your process wherever it is and handles all windows messages. So
imagine the scenario where the user clicks a button, some heavy work hits,
and the gui goes non responsive. The user starts clicking around to see if
it's dead. Then you call App.DoEvents, and your intensive process is paused
while all of their clicks are processed. Now imagine that one of those
clicks they made was on the button that starts the heavy work. Now you're
running two copies of the heavy code, one execution occurring inside the
other.

Another scenario - imagine a database bound form with a save and close
button. The user clicks save, then while that's going, clicks cancel, and
you call App.DoEvents, the cancel event goes through and you close the form.
Then the save finishes and you go back to update the state of the form...
App.DoEvents should be used with caution.

Niall

george said:
I am writing a process that is a tight loop that consumes alot of cpu.
Back in my VB days I could use "DoEvents" to periodically yield control to
the OS incase there were any pending events. This would keep the app from
being able to effectively take over the system. What is the equivilant in
C#?
 
S

Sherif ElMetainy

Hello

You can also run the loop in a low priority thread.
Best regards,
Sherif
george said:
I am writing a process that is a tight loop that consumes alot of cpu.
Back in my VB days I could use "DoEvents" to periodically yield control to
the OS incase there were any pending events. This would keep the app from
being able to effectively take over the system. What is the equivilant in
C#?
 
K

KJ

To yield CPU time, use Thread.Sleep

To do the repaint (which doesn't work very well) try Control.Refresh

To do things right, run the tight loop on a thread other than the main
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top