如何在 C# 中的特定时间生成警报?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1297109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I generate an alert at a specific time in C#?
提问by Mobin
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).
如何在特定时间生成事件?例如,假设我想在上午 8:00 生成一个警报,通知我上午 8:00(或在任何给定时间通知我当前时间的事件)。
采纳答案by dtb
Use the System.Threading.Timerclass:
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.
该callback
代表将被称为下一次它是上午8:00,此后每隔24小时。
See this SO questionhow to calculate the next 8:00 AM occurrence.
请参阅此 SO 问题如何计算下一个上午 8:00 发生的事件。
回答by Mobin
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
我们可以使用此功能让计时器运行多次或将其更改为在特定时间运行:D
回答by ChrisF
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.
警报是否必须由您的程序生成?另一种方法是使用计划任务(在 Windows 中)来生成警报。您可能需要编写一个小程序来从主应用程序的数据文件等中收集任何信息。
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.
- 您不必编写任何代码来支持该功能。您可以使用操作系统中内置的东西。该示例适用于 Windows,但其他操作系统具有相同的功能。您可以将开发和支持工作集中在您自己的代码上。
- 您的主应用程序不必运行即可触发任务。
回答by Thunder
Elaborating on dtb's answer this is how I implemented it.
详细说明 dtb 的答案,这就是我实现它的方式。
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");
}