1

To put it simply,

I start running my C# program in the morning, and the program should show the user a message at 5:45 PM. How can I do this in C#?

Edit: I asked this question because I thought using a timer is not the best solution (comparing the current time periodically to the time I need to run the task):

private void timerDoWork_Tick(object sender, EventArgs e)
{
    if (DateTime.Now >= _timeToDoWork)
    {

        MessageBox.Show("Time to go home!");
        timerDoWork.Enabled = false;

    }
}
user1126360
  • 403
  • 1
  • 4
  • 8

4 Answers4

5

I asked this question because I thought using a timer is not the best solution (comparing the current time periodically to the time I need to run the task)

Why? why not timer a best solution? IMO timer is the best solution. but not the way you have implemented. Try the following.

private System.Threading.Timer timer;
private void SetUpTimer(TimeSpan alertTime)
{
     DateTime current = DateTime.Now;
     TimeSpan timeToGo = alertTime - current.TimeOfDay;
     if (timeToGo < TimeSpan.Zero)
     {
        return;//time already passed
     }
     this.timer = new System.Threading.Timer(x =>
     {
         this.ShowMessageToUser();
     }, null, timeToGo, Timeout.InfiniteTimeSpan);
}

private void ShowMessageToUser()
{
    if (this.InvokeRequired)
    {
        this.Invoke(new MethodInvoker(this.ShowMessageToUser));
    }
    else
    {
        MessageBox.Show("Your message");
    }
}

Use it like this

 SetUpTimer(new TimeSpan(17, 45, 00));
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
2

You can use Task Scheduler too.

Also there is a Timer class which can help you

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

You can use a Timer to check each minute if DateTime.Now == (the specific time you want)

This is an example of a code with windows forms

public MainWindow()
    {
        InitializeComponent();
        System.Windows.Threading.DispatcherTimer timer_1 = new System.Windows.Threading.DispatcherTimer();
        timer_1.Interval = new TimeSpan(0, 1, 0);
        timer_1.Tick += new EventHandler(timer_1_Tick);
        Form1 alert = new Form1();
    }
    List<Alarm> alarms = new List<Alarm>();

    public struct Alarm
    {
        public DateTime alarm_time;
        public string message;
    }


    public void timer_1_Tick(object sender, EventArgs e)
    {
        foreach (Alarm i in alarms) if (DateTime.Now > i.alarm_time) { Form1.Show(); Form1.label1.Text = i.message; }
    }
Hadron
  • 441
  • 3
  • 13
  • Why to check each minute? can't we set timer to desired time? – Sriram Sakthivel Aug 16 '13 at 09:08
  • I thought it would be better to create a timer with a 1 minute (for example) interval with the tick event handler to bring the program to check each minute for correspondancy. It's going to be way easier after if you want to set more than one "alarm". – Hadron Aug 16 '13 at 09:34
  • I don't think so, check out my solution http://stackoverflow.com/a/18270238/2530848 – Sriram Sakthivel Aug 16 '13 at 09:41
  • What I mean is, if you mish to set more than one alarm, you'll be creating one timer per alarm, on the other hand, I acknowledge that checking also gets heavier the more things you've got to check – Hadron Aug 16 '13 at 10:15
  • Not one timer per alarm, I'll set first alarm time, when that event elapsed I'll set same timer to next interval. Doing so we can reuse the timer and avoid checking per second too – Sriram Sakthivel Aug 16 '13 at 10:32
  • I agree, that would be the best solution – Hadron Aug 16 '13 at 12:35
0

You may easily implement your own alarm class. To start with, you may want to check the Alarm class at the end of the MS article.

Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78