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

前端 未结 4 1538
走了就别回头了
走了就别回头了 2020-12-03 03:10

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 curre

4条回答
  •  星月不相逢
    2020-12-03 03:31

    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.

提交回复
热议问题