running something every 6hrs, 1min

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
what would be the best way to run a process every 6 hours and 1 minute

i've heard that using the timer control past 3 minutes it gets inaccurate so i was thinking of somehow storing a timestamp in a variable and every minute checking it against the clock to see if it's been 6hours and 1 minute

but i'm not sure exactly on how to accommodate for a new day (when it spans to the next day) so it'll be accurate

any suggestions?
 
VB.NET:
	'Set the time to the minimum possible so the operation occurs on the first tick.
	Private lastOperationTime As Date = Date.MinValue

	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		If DateTime.Now.Subtract(lastOperationTime).TotalMinutes > 6 * 60 + 1 Then
			'Initiate operation here.

			Me.lastOperationTime = Date.Now
		End If
	End Sub
 
kulrom said:
Where and when you've heard it ... the thing about timer? i've never heard that using the timer control past 3 minutes it gets inaccurate ... strange if i may :)

Cheers ;)

i heard it from someone on another forum, but i didnt know if it was true or not

anywho thanx jmcilhinney i'll try that later tonight when i get home
 
I provided that code based on what you asked for, but I also am not aware of any specific issue with a Timer with a large Interval. Perhaps you should do some testing by comparing the two methods for an interval of a couple of hours or so over a period of a couple of days and see what the average discrepancy is.
 
hi ,
1 More thing to Help you ,

'Create a TimeSpan of 6hrs and i min
Dim tmspan as new Timespan(6,1,0)

' Create a Timer from System.Threading.Timers and set Timer.Change(tmspan,tmspan).

The timer will fire after every 6hrs 1 min..

Thanks
Jay
 
From the help documentation:

System.Windows.Forms.Timer:
This timer is optimized for use in Windows Forms applications and must be used in a window... This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing.
System.Timers.Timer:
The Timer component is a server-based timer... The server-based Timer is designed for use with worker threads in a multithreaded environment. Server timers can move among threads to handle the raised Elapsed event, resulting in more accuracy than Windows timers in raising the event on time.
System.Threading.Timer:
System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by threadpool threads. You might also consider System.Windows.Forms.Timer for use with Windows forms, and System.Timers.Timer for server-based timer functionality. These timers use events and have additional features.
 
Back
Top