0

How can I call a MessageBox.Show command when the time is exactly 5pm in C#?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    const double interval60Minutes = 1000; // milliseconds to one hour
    System.Timers.Timer checkForTime = new System.Timers.Timer(interval60Minutes);

    string Check = null;
    private void Form1_Load(object sender, EventArgs e)
    {
        checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
        checkForTime.Enabled = true;
    }


    public void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
    {

        DateTime target = new DateTime(2016, 5, 8, 11, 58, 0);
        DateTime now = DateTime.Now;

        if (target.ToString("HH:mm:ss") == now.ToString("HH:mm:ss") && (Check == null))
        {
            MessageBox.Show("Welcome Admin");
            checkForTime.Stop();
            checkForTime.Enabled = false;
        }
    }
}

If I put just MessageBox.Show without the if statement it works, but when I put a condition nothing happens. The timer won't stop I used both enabled = false and stop function.

Mostafiz
  • 7,243
  • 3
  • 28
  • 42
Ryze2
  • 31
  • 1
  • 4
  • 2
    The odds that the time matches *exactly* within 100 nanoseconds are very, very low. Also rather a bad idea to use System.Timers.Timer, good odds that the message box disappears behind another window since the Elapsed event runs on a worker thread. Drop a Timer from the toolbox. Calculate the Interval so you need only one Tick. And use NotifyIcon.ShowBalloonTip() to tell the user. – Hans Passant May 08 '16 at 15:45
  • Look at this answer to a similar question: http://stackoverflow.com/a/18270238/820068 – Wesley Long May 08 '16 at 16:05
  • why you are referring a link which is close as off topic ? – Mostafiz May 08 '16 at 16:07
  • @MostafizurRahman - actually the question is closed because of a user not demonstrating minimal understanding, not because it is off-topic. Hopefully it will someday get addressed, but I understand the mods have a lot more to deal with on stackoverflow than we do over in workplace. However, the answer I provided the link to is almost exactly what I was going to answer with, so it is, IMO, a duplicate. – Wesley Long May 08 '16 at 16:13
  • use my update I have added how to stop timer – Mostafiz May 08 '16 at 16:31
  • @MostafizurRahman For some reason I can't access the checkForTime.Stop(); in the method checkForTimeElapsed – Ryze2 May 08 '16 at 16:39
  • put your timer in global scope see my update – Mostafiz May 08 '16 at 16:44
  • @MostafizurRahman brother I used your answer but it wont stop the timer. It keep showing the messagebox. Please see my updated question.. – Ryze2 May 08 '16 at 16:52
  • ok let me check again – Mostafiz May 08 '16 at 16:53
  • @Ryze2 brother I have just check your code and its working fine here, please look carefully if the is problem in somewhere else – Mostafiz May 08 '16 at 17:01
  • @MostafizurRahman so the messagebox only shows up once? – Ryze2 May 08 '16 at 17:03
  • move your target time outside the `checkForTime_Elapsed` method because both target time and present time is initialize inside the `checkForTime_Elapsed` so everytime it true, for this messegebox will show up every time, move outside then it will only show whne the time match – Mostafiz May 08 '16 at 17:07
  • your `ToString("01:05")` code is wrong I going to edit it – Mostafiz May 08 '16 at 17:09
  • @MostafizurRahman I put all the code in the question box please tell me what I did wrong thank you brother – Ryze2 May 08 '16 at 17:10
  • got it you are stoping the timer insdie the callback function for that you have to set `checkForTime.AutoReset = false;` in your code see my latest update – Mostafiz May 08 '16 at 17:33
  • Okay thank you brother – Ryze2 May 08 '16 at 17:40

1 Answers1

0

Use this, this code will check if your target time and present time is same or not if same then it will run the MessageBox

if (target.ToString("HH:mm:ss") == now.ToString("HH:mm:ss"))
   {
       MessageBox.Show("It's Time");
   }

use

checkForTime.Stop();

where you want to stop the timer

to access the timer from another method put your timer in the global scope

    const double interval60Minutes = 1000; // milliseconds to one hour
    System.Timers.Timer checkForTime = new System.Timers.Timer(interval60Minutes);

    string Check = null;
    private void Form1_Load(object sender, EventArgs e)
    {
        checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
        checkForTime.Enabled = true;
        checkForTime.AutoReset = false;
    }


    public void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
    {

        DateTime target = new DateTime(2016, 5, 8, 11, 58, 0);
        DateTime now = DateTime.Now;

        if (target.ToString("HH:mm:ss") == now.ToString("HH:mm:ss") && (Check == null))
        {
            MessageBox.Show("Welcome Admin");
            checkForTime.Stop();
        }

    }
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • Updated time format to `HH:mm:ss` to be simpler. – Gabor May 08 '16 at 15:59
  • reason for downvote please it will improve my answer – Mostafiz May 08 '16 at 16:45
  • 1
    Directly comparing times like that is sketchy. Comparing whole seconds will probably work, but there's no guarantee that the program/system won't choke and miss the time. A better way is to check whether the current time is past or equal to a certain time. Also converting to strings and comparing seems like a waste of resources. – Chris May 08 '16 at 16:56