Home > DeveloperSection > Forums > Timer not detecting when input matches DateTime.Now
Royce Roy
Royce Roy

Total Post:134

Points:938
Posted on    December-23-2013 2:52 AM

 C# C# 
Ratings:


 1 Reply(s)
 662  View(s)
Rate this:

I have a timer on my form with an interval of 1000 (1 second). I am allowing the user to input a time via three comboboxes. The time they input is then converted to the DateTime format. Finally, the timer checks every tick if the input time equals DateTime.Now. The problem is that it's not working.

Say I set the alarm to 9:00:00 PM when it's 8:59:50 PM. I click Start and wait ten seconds for the alarm to go off, but it never does. When debugging the code, both numbers are the same, meaning that it should fire, but it doesn't.

Here's my code:

private void tmrAlarm_Tick(object sender, EventArgs e) {

    DateTime alarmTime = Convert.ToDateTime(statusAlarm.Text); // say statusAlarm.Text is 9:00:00 PM

    if(alarmTime == DateTime.Now) {

        MessageBox.Show("bzzt");

    }

}



Pravesh Singh

Total Post:411

Points:2881
Posted on    December-23-2013 5:10 AM

Hi Royce,

You could also use

DateTime.Now.ToShortTimeString() method for checking equality

Don't want to miss updates? Please click the below button!

Follow MindStick