Link to home
Start Free TrialLog in
Avatar of PAI_03
PAI_03Flag for United States of America

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?
Avatar of kaufmed
kaufmed
Flag of United States of America image

You'll have to poll the system time. If you set the interval of your tick small enough, you should get decent accuracy.
Avatar of PAI_03

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
    }
}

Open in new window

Do you mean you want to start the timer at a specific time instead of starting it straight away?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PAI_03

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.
Avatar of PAI_03

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

Open in new window

Hour = 12 means noon, not midnight. Midnight = 0.
Avatar of PAI_03

ASKER

oh thank you for that. I'm still in the learning process of vb.net
Avatar of PAI_03

ASKER

Thank you for the example code. worked perfect.