-1

I want to execute an event at a specific time

I added a timer with 800 ms , and in the timer event I compared between the set value and the current time but the problem is that the event happens more than ı time,as it is shown in the photo,due to the code the messagebox must occure 1 time, but when i execute it happens 7 or more times the code is:

private void timer2_Tick(object sender, EventArgs e)
{
    DateTime date1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, Convert.ToInt16(Properties.Settings.Default.shift_1_end_hh), Convert.ToInt16(Properties.Settings.Default.shift_1_end_min), 0);
    DateTime date2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
    int result = DateTime.Compare(date1, date2);
    if (result == 0)
    {
        MessageBox.Show("tttt");
    }

}

https://i.stack.imgur.com/MIyfk.png

Md. Zakir Hossain
  • 1,082
  • 11
  • 24
Memduh
  • 3
  • 1
  • If you want to stop the timer from raising any more events, disable it with timer2.Enabled = false; You can do this in that method. – Esko Dec 27 '17 at 11:41
  • 1
    The code executes every 800 milliseconds so you will get a lot of message boxes. – nicomp Dec 27 '17 at 11:41
  • 1
    Possible duplicate of [C# - how do you stop a timer?](https://stackoverflow.com/questions/4563704/c-sharp-how-do-you-stop-a-timer) – mjwills Dec 27 '17 at 11:41
  • 1
    Checking for when an exact time is hit is generally a bad idea. A big interval could miss the time, and a small interval could cause multiple events as you have seen. It is better to check if the current time is _greater than or equal_ to the target time, raise the event once, then set [`Timer.Enabled = false;`](https://msdn.microsoft.com/en-us/library/system.timers.timer.enabled(v=vs.110).aspx) – Rhumborl Dec 27 '17 at 11:43
  • What is the "specific time" ? When you want to raise an event ? – lucky Dec 27 '17 at 11:46
  • ı tested the same code in other a new project and it worked normally, but in my projrct it works wrongly with multipple messageboxes; ı m wondering the problrm from th compiler or from the assembly! @esko, – Memduh Dec 27 '17 at 14:17

3 Answers3

1

Here is a better version of your code:

private void timer2_Tick(object sender, EventArgs e)
{
    var hours = Convert.ToInt16(Properties.Settings.Default.shift_1_end_hh);
    var minutes = Convert.ToInt16(Properties.Settings.Default.shift_1_end_min);
    var date1 = DateTime.Today.AddHours(hours).AddMinutes(minutes);
    int result = DateTime.Compare(date1,DateTime.Now);
    if (result == 0)
    { 
         timer2.Enabled = false;
         MessageBox.Show("tttt"); 
    }
}

Points of interest:

  • Simplified the creation of date1
  • Removed date2 and simply use DateTime.Now instead
  • Disabled the timer once the condition is met.
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • when ı used compare as time the message didnt appear, but when i converted to string and compared the messeage box appeared like the following code: – Memduh Dec 27 '17 at 14:12
  • var hours = Convert.ToInt16(num_shift_1_end_hh.Value); var minutes = Convert.ToInt16(num_shift_1_end_min.Value); var seconde = 0; var date1 = DateTime.Today.AddHours(hours).AddMinutes(minutes).AddSeconds(seconde); var date2 = DateTime.Now; string date1_str = date1.ToString(); string date2_str = date2.ToString(); int result = string.Compare(date1_str, date2_str); if (result == 0) { MessageBox.Show(result.ToString()); } – Memduh Dec 27 '17 at 14:15
  • In fact, you don't even need the DateTime, you can compare `DateTime.Now.Hours` and `DateTime.Now.Minutes` with the values from your settings. I was only comparing dates since I've copied most of your code. – Zohar Peled Dec 27 '17 at 15:06
0

Here you can find better solution [How to compare dates in c#

0

Try this

 if (result == 0)
{ 
     if (timer2.IsEnabled)
     {
        timer2.Stop();
     }
     MessageBox.Show("tttt"); 
}
Md. Zakir Hossain
  • 1,082
  • 11
  • 24