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?
PAI_03Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You'll have to poll the system time. If you set the interval of your tick small enough, you should get decent accuracy.
0
PAI_03Author Commented:
do you think you can give me an example code please? thanks
0
käµfm³d 👽Commented:
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

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

CodeCruiserCommented:
Do you mean you want to start the timer at a specific time instead of starting it straight away?
0
käµfm³d 👽Commented:
VB version:

Private Sub timer1_Tick(sender As Object, e As EventArgs)  ' Interval set to 1/2 second (500 milliseconds)
    Dim now As DateTime = DateTime.Now

    If now.Hour = 9 AndAlso now.Minute = 0 AndAlso now.Second = 0 Then  ' if it's 9 AM
        ' do something
    End If
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PAI_03Author Commented:
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.
0
PAI_03Author Commented:
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

0
käµfm³d 👽Commented:
Hour = 12 means noon, not midnight. Midnight = 0.
0
PAI_03Author Commented:
oh thank you for that. I'm still in the learning process of vb.net
0
PAI_03Author Commented:
Thank you for the example code. worked perfect.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.