-2

I want to Create a program in C#.net that will execute or run a command on a given time in my DateTimePicker.

Example I want to execute my command on 06:30 PM it is not 06:30 PM yet. Program will wait until 6:30 PM then it will execute my command.

I'm sorry. I am just newbie here.

Can you please help me?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Novice
  • 183
  • 3
  • 14

3 Answers3

2

You could use a System.Timers.Timer to run every second and check whether some task is to be run. Like this for example:

private System.Timers.Timer _timer;

_timer = new System.Timers.Timer();
_timer.Interval = 1000;
_timer.AutoReset = false;       // Fires only once! Has to be restarted explicitly
_timer.Elapsed += MyTimerEvent;
_timer.Start();

private void MyTimerEvent(object source, ElapsedEventArgs e)
{
    DateTime now = DateTime.Now;

    // Check for tasks to be run at this point of time and run them
    ...

    // Don't forget to restart the timer
    _timer.Start();
}

In the timer method you may have to truncate the seconds. You'll also need to make sure that the task is not run every second during the minute you want it to run at.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

You have two options: 1) Use task scheduler(windows -> search 'task scheduler'). 2) Just create a timer in your code, that every minute fire an event that check the time.

Kram
  • 526
  • 5
  • 11
  • The task scheduler runs an entire application, not just some functionality. You could use a command line parameter to tell the program what to do, but it's easier to use a timer. – Thorsten Dittmar Jun 16 '15 at 08:02
0

You can use the TaskScheduler class

The link also provides a really good example of usage.

Rikard Söderström
  • 1,000
  • 6
  • 14