Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Yield Processor Time (ie, doevents)

Status
Not open for further replies.

byrne1

Programmer
Aug 7, 2001
415
US
I have a service that should fire an event every 10 minutes. I originally placed a timer in the service but it's functionality was VERY flaky in that sometimes it would fire, but most of the time it would not.

So, I put a do loop in the service that basically said "initially set a start time, loop until datediff function indicates 10 minutes has passed, fire the event, reset the start time, and start over."

Now my problem is that this service, when it's running, is eating up CPU cycles, constantly running 97-99%. I tried a System.Threading.Thread.Sleep command but this did not change the performance of the service (it still ate up my CPU cycles).

Is there anything else I can try???
 
The DoEvents methods exists in:

System.Windows.Forms.Application.DoEvents()

I'm not sure this will take care of your CPU utilization since you'll still be in a tight loop though. Any idea what was causing the Timer to be flaky?
 
I have no idea what's happening with the timer. I don't get an error message or anything, just that the elapsed event does not fire. And although it's not consistent, it seems to not work more than it does work.

Also, regarding the System.Windows.Form.Application.DoEvents(), I'm using a service so this is not available for me to use.
 
You're right about the doevents in the service, wasn't reading real close :/

Did you use the SetTimer() function to create the timer and if so, how long of a delay did you give it? Maybe the delay was too long, try setting it to a smaller delay and the use the datediff to determine the 10 minutes.
 
You must use threading in your service. You can spawn your code to be executed on the new thread and the os will manage cpu usage and divide it up among all the threads. If you look at the windows task manager performance tabs you will see all the threads your computer has running.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top