|
-
Jan 5th, 2007, 05:19 AM
#1
Thread Starter
Hyperactive Member
[02/03] System.Timers.Timer and System.Windows.Forms.Timer
Just a bit of advice about timers.
I have an event I want to fire aprox every minute, so have put this on an a windows.form timer with an interval of 60000.
I then have other events I want to fire at specific times, so I put this on a System.Timers.Timer with the SynchronizingObject set to none.
I then fire the events at the correct times like so
VB Code:
curmin = Minute(Now())
If curmin = 0 Then
curhour = Hour(Now())
If curhour = 0 Then
curday = Weekday(Now())
End If
End If
Select Case curhour
Case 8
If curmin = 1 Then
deloldmes()
End If
If curmin = 21 Then
gettftp(1)
End If
If curmin = 56 Then
getttp(2)
End If
Case 9
If curmin = 1 Then
getmonmes()
deloldmes()
End If
Case 10
.
.
.
end select
I assume I have to be careful because in theory the two timers could fire at the same time so access the same procedures. Is this the only thing I need worry about and is this the correct way of doing what I'm doing?
Cheers
-
Jan 5th, 2007, 06:42 AM
#2
Re: [02/03] System.Timers.Timer and System.Windows.Forms.Timer
There's no reason that you have to use a Timers.Timer at all. You can just use Windows.Forms.Timers for everything and just Stop it in its Tick event. Alternatively you can set the SynchronizingObject of the Timers.Timer to the form and it will raise its Elapsed event in the UI thread, ensuring that its handlers cannot be executed at the same time as the Windows.Forms.Timer's Tick event handler.
Having said that, there is nothing wrong with the way you're doing things but you need to recognise the fact that the Timers.Timer will raise its Elapsed event in a worker thread. In that case you need to use standard thread synchronisation techniques if there is any chance that they may interfere with each other.
-
Jan 5th, 2007, 07:11 AM
#3
Thread Starter
Hyperactive Member
Re: [02/03] System.Timers.Timer and System.Windows.Forms.Timer
My worry is that I could lose a minute if I had 2 windows.form timer, timer 1 fires every second, timer 2 fires every minute. Is the below possible, and what's the best way around this
timer2 fires at 12:20:59 seconds
curmin = 20
timer1 fires
does some work
timer 2 fires at 12:22:00
curmin = 22
So the event due to fire on the 21st minute never will
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|