How do I generate an alert at a specific time in C#?

Mobin picture Mobin · Aug 19, 2009 · Viewed 28.1k times · Source

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).

Answer

dtb picture dtb · Aug 19, 2009

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.