|
-
Mar 10th, 2010, 02:07 PM
#1
Thread Starter
Addicted Member
Timer event
Hello everybody!
I have timer control on my form, and tick is 1000, which means that timer every second do code from it's tick event.
I'm interesting how to do something when minutes changing.
For example, now is 8:43, and I want program every second check is now 8:44, and when is 8:44 program have to do code from timers tick event.
8:43 is not constant, 8:43 is variable.
-
Mar 10th, 2010, 03:43 PM
#2
Re: Timer event
Does it have to be on the sytem time minute, or do you just want to run some additional code every 60 seconds. If it is the latter, just add a counter variable and when it reaches 60, clear it, and do whatever you want. If it is based on the system time, then I think the easiest solution would be to save Date.Now.Minutes to a variable, and on every timer tick, check to see whether Date.Now.Minutes is the same as what you saved.
My usual boring signature: Nothing
 
-
Mar 11th, 2010, 07:22 AM
#3
Thread Starter
Addicted Member
Re: Timer event
I solved my problem!
Tick of my timer is 1000 (so check every second)
Dim second as Integer
second = Now.Second
If second = 0 then
'minutes changed
Else
End If
End Sub
That's it, and timer doesn't ballast processor.
-
Mar 11th, 2010, 07:43 AM
#4
Re: Timer event
There is a slight problem with that. Because the Tick event is raised on the UI thread, it may get queued and you may have more than a second between Ticks. It's a slight risk but you may actually go straight from 59 to 01, missing the minute change.
-
Mar 11th, 2010, 07:51 AM
#5
Re: Timer event
Timers shouldn't be used to time things for the reasons JMC pointed out. Use a StopWatch for that. In your case use a combination of both.
-
Mar 11th, 2010, 10:49 AM
#6
Re: Timer event
I'd say just check the minute and watch for that to change.
My usual boring signature: Nothing
 
-
Mar 11th, 2010, 12:18 PM
#7
Re: Timer event
This method has served me well.
Code:
Dim stpw As New Stopwatch
Dim IntervalWatchingFor As TimeSpan = New TimeSpan(0, 0, 1)
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = CInt(IntervalWatchingFor.TotalMilliseconds / 2)
stpw.Reset()
stpw.Start()
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
'if the stopwatch is >= IntervalWatchingFor then...
End Sub
-
Mar 11th, 2010, 12:37 PM
#8
Re: Timer event
Why use a StopWatch? Just checking dates uses much less code, and is less complicated:
vb.net Code:
Private LastMinute As Date = Date.Now Private Sub Timer1_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Timer1.Tick If Date.Now.Subtract(LastMinute).TotalMinutes >= 1 Then LastMinute = Date.Now ' Do whatever End If End Sub
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
|