PAI_03
asked on
how to base timer from system time
How would i go along setting the tick event of a timer to be based off a specific time?
You'll have to poll the system time. If you set the interval of your tick small enough, you should get decent accuracy.
ASKER
do you think you can give me an example code please? thanks
It's quite simple: check the system time in the tick handler; execute your logic if the time is at the target.
private void timer1_Tick(object sender, EventArgs e) // Interval set to 1/2 second (500 milliseconds)
{
DateTime now = DateTime.Now;
if (now.Hour == 9 && now.Minute == 0 && now.Second == 0) // if it's 9 AM
{
// do something
}
}
Do you mean you want to start the timer at a specific time instead of starting it straight away?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
well i want the timer to start when the "start" button was been pressed but i want the tick even to be on 12:00 am. What I am creating is basically how many days have passed since out shipping department has had since there last error. Almost like a "how many days since last accident sign" but a program that they can monitor in there department on a computer.
ASKER
Here is what i have along with the code added from kaufmed's example
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
Timer1.Enabled = True
btnStart.Enabled = False
End Sub
Private Sub btnStop_Click(sender As System.Object, e As System.EventArgs) Handles btnStop.Click
Timer1.Enabled = False
btnStart.Enabled = True
End Sub
Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
Timer1.Enabled = False
lblTime.Text = 0.0
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim now As DateTime = DateTime.Now
If now.Hour = 12 AndAlso now.Minute = 0 AndAlso now.Second = 0 Then
lblTime.Text = lblTime.Text + 1
End If
End Sub
End Class
Hour = 12 means noon, not midnight. Midnight = 0.
ASKER
oh thank you for that. I'm still in the learning process of vb.net
ASKER
Thank you for the example code. worked perfect.