Reminder / Scheduler best way to check time.

Shazbot
01-03-2005, 05:51 AM
Hi

I just wanted to know especially if you have done this before, the best way to get the time every min with lowest cpu usage etc.

* Timer
* Thread
* Other?

Regards
-Shazza

Tygur
01-03-2005, 11:32 AM
I would expect it to depend on your circumstances. You can do a thread whose ThreadStart looks something like this:
Sub MyThreadStart()
Do
'put your code here that gets the time
Threading.Thread.Sleep(New TimeSpan(0, 1, 0))
Loop
End Sub


But that would depend on how long it takes to execute the code you insert. If it takes long enough, your code won't fire every minute, because the next time it runs is a minute after the last time it finishes, not the last time it starts. That can be fixed, tho:
Sub MyThreadStart()
Do
Dim StartTime As DateTime = Now
'put your code here that gets the time
Threading.Thread.Sleep(StartTime.AddMinutes(1).Subtract(Now))
Loop
End Sub


The Timer that's in System.Windows.Forms is probably a better idea if your code that gets the time needs to access the form or other GUI elements. That way, there's no threading issues. But whether or not the timer reliably fires each and every minute on time depends on how busy the GUI thread actually is. That timer can be set to fire once a minute by setting the Interval to 60,000.

There's also a Timer in System.Threading, but I've never used it. It's more similar to the first method I described, except the thread is created behind the scenes. The code that runs once a minute will still run in a separate thread.

I would expect any difference in CPU usage when using the above methods to be insignificantly small.

Shazbot
01-03-2005, 04:53 PM
Thanks for your input.

Regards
- Shazza

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum