How can i generate an event at a specific time? For example, say I want to generate an alert at 8:00 AM that informs me its 8:00 AM (or an event that informs me of the current time at any given time).
Asked
Active
Viewed 3.0k times
4 Answers
47
Use the System.Threading.Timer class:
var dt = ... // next 8:00 AM from now
var timer = new Timer(callback, null, dt - DateTime.Now, TimeSpan.FromHours(24));
The callback
delegate will be called the next time it's 8:00 AM and every 24 hours thereafter.
See this SO question how to calculate the next 8:00 AM occurrence.
-
4I know following the link mentions it, but might want to clarify that that is System.Threading.Timer and not System.Windows.Forms.Timer – McAden Aug 18 '09 at 23:33
-
1Would it work if I provide the last 8.00 from now? So a negative number is given? – Jaanus Mar 20 '13 at 20:31
-
@Jaanus No I just tried it and it throws an exception. If dt < DateTime.Now then do dt.AddDays(1). – Brian Tacker May 23 '13 at 16:21
-
1This seems to be unreliable in practice. I have implemented some code using this timer class from the Threading namespace. Its designed to roll over a log file at a specific time. In our case, midnight. We noticed that in production, sometimes the rollover happens slightly before, or slightly after midnight. I guess it's good enough for most implementations, but it does seem that the time of execution can vary slightly from invocation to invocation. – Mark W Feb 05 '16 at 15:07
9
Elaborating on dtb's answer this is how I implemented it.
private void Form1_Load(object sender, EventArgs e)
{
System.Threading.TimerCallback callback = new TimerCallback(ProcessTimerEvent);
//first occurrence at
var dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 10, 0, 0);
if (DateTime.Now < dt)
{
var timer = new System.Threading.Timer(callback, null,
//other occurrences every 24 hours
dt - DateTime.Now, TimeSpan.FromHours(24));
}
}
private void ProcessTimerEvent(object obj)
{
MessageBox.Show("Hi Its Time");
}
1
Does the alert have to be generated by your program? An alternate approach is to use a Scheduled Task (in Windows) to generate the alert. You might need to write a small program that gathers any information from your main application's data files etc.
There are a couple of main benefits to using this approach:
- You don't have to write any code to support the feature. You make use of something built into the operating system. The example is for Windows, but other OSes have the same feature. You can concentrate your development and support effort on your own code.
- Your main application doesn't have to be running for the task to fire.

ChrisF
- 134,786
- 31
- 255
- 325
0
private void Run_Timer()
{
DateTime tday = new DateTime();
tday = DateTime.Today;
TimeSpan Start_Time = new TimeSpan(8,15,0);
DateTime Starter = tday + Start_Time;
Start_Time = new TimeSpan(20, 15, 0);
DateTime Ender = tday + Start_Time;
for (int i = 0; i <= 23; i++)
{
Start_Time = new TimeSpan(i, 15, 0);
tday += Start_Time;
if (((tday - DateTime.Now).TotalMilliseconds > 0) && (tday >= Starter) && (tday <= Ender))
{
Time_To_Look = tday;
timer1.Interval = Convert.ToInt32((tday - DateTime.Now).TotalMilliseconds);
timer1.Start();
MessageBox.Show(Time_To_Look.ToString());
break;
}
}
}
We can use this function for getting timer running for times or change it to run on a specific time :D
-
-
3But its also obvious that you didn't get the idea that a person who asks the question better understand his question .I agree he gave me a format for doing that on one specific time of day but i had to make it run on some conditions not statically on a certain time of day once most of the work done in my code is for running that loop and set the time for next timer stop .If you can see that not the number of lines in that – Mobin Mar 20 '10 at 21:25