如何在每晚03:00触发事件?
如何做到这一点最好?我想要一个在服务器上运行的应用程序每天晚上 03:00 触发一个事件。
How is this done best? I want an app that's running on a server to trigger an event every night at 03:00.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用Windows任务计划程序
Use windows task scheduler
如果您想在运行应用程序代码时执行此操作(而不是使用任务计划程序),则应选择一个持续时间以使您的应用程序休眠相当长的时间(例如,1 小时或 3,600 秒)。您的应用程序会循环,并且当每个睡眠调用到期时,应用程序会定期检查距截止时间 (03:00) 还剩多少时间。一旦剩余睡眠时间低于粗略间隔时间,就应该减少到更短的间隔(每次减半,或减少到10秒)。继续循环,休眠并减少间隔时间,直到达到目标截止时间。
这可以防止循环过于频繁地唤醒(86,400 次 1 秒间隔太过分了),但它也可以防止应用程序循环因睡眠时间过长而超出目标截止时间。
If you want to do this in running app code (instead of using a task scheduler), you should choose a duration to let your app sleep that's fairly long (e.g., 1 hour, or 3,600 sec). Your app loops, and as each sleep call expires, the app periodically checks how much time is left until the deadline time (03:00). Once the remaining sleep time gets below the coarse interval time, it should be reduced to a shorter interval (halved each time, or reduced to 10 sec). Continue the loop, sleeping and reducing the interval time, until the target deadline time is reached.
This prevents the loop from waking up too often (86,400 1-sec intervals is overkill), but it also prevents the app loop from overshooting the target deadline time by sleeping too long.
您可以制作一个间隔为 1 秒的计时器,当计时器响起时,检查是否是 3:00。
You could make a timer with an interval of 1 second and when the timer goes off, check if it's 3:00.
您需要在服务中构建它,以确保即使没有人登录计算机也能运行,然后有很多不同的方法来确保触发器发生。
考虑创建一个
System.Timers.Timer
,其中Interval
设置为DateTime.Now
与下一个 3:00 之间的差值。You'll need to build it in a service in order to ensure that it runs even if there's nobody logged into the machine, and then there are lots of different methods to ensure that the trigger occurs.
Consider making a
System.Timers.Timer
where theInterval
is set to the difference betweenDateTime.Now
and the next 3:00.这里有两个基本选项。
如果您尝试在现有服务中执行此操作,可以使用 计时器在每晚 3:00 触发自己,并运行您的“任务”。
话虽这么说,这通常可以通过 Windows 任务计划程序更好地处理。您只需安排它每天 3:00 运行一次,而不是让应用程序 24/7 保持活动状态。
编辑:
如果您需要从代码中使用任务计划程序(在另一条评论中提到),这也是可能的。任务计划程序提供 API 用于设置通过任务计划程序执行单个任务 (ITask) (ITaskScheduler)。
然而,考虑到您正在使用 XP Embedded,您可能最好只使用正常的系统配置功能,并设置每天运行一次的任务。在嵌入式系统中,您应该在部署期间拥有足够的控制权来执行此操作。
There are two basic options here.
If you're trying to do this within an existing service, you can use a Timer to trigger yourself at 3:00 each night, and run your "task".
That being said, this is typically better handled via Windows Task Scheduler. Instead of keeping the application alive 24/7, you just schedule it to run once every day at 3:00.
Edit:
If you need to work with the Task Scheduler from code (mentioned in another comment), that is also possible. The Task Scheduler provides an API for setting up individual Tasks (ITask) via the Task scheduler (ITaskScheduler).
However, given that you're working on XP Embedded, you're probably better off just using the normal system configuration capabilities, and setting up a task to run once each day. In an embedded system, you should have enough control during your deployment to do this.
这是我们编写的服务的简化版本,该服务每 60 秒运行一个计时器来监视表...您可以更改计时器经过事件来检查时间并然后运行它:
显然,如果可以的话,请使用该任务就像这里其他人提到的那样。这是执行此操作的首选方式。我碰巧有这段代码,所以我想我会把它发布出来,以防它对你有帮助。此外,这段代码对我们有用,因为我们永远不知道外部源何时会编辑表。正如 md5sum 所指出的,将间隔设置为正确的毫秒数将是一种更有效的方法。
Here is a simplified version of a service that we wrote that runs a timer every 60 seconds to watch a table... you could alter the timer elapse event to check the time and run it then:
Obviously, if you can, use the task scheduler like everyone else here has mentioned. It is the preferred way of doing this. I just happened to have this code laying around so I thought I'd post it in case it could be helpful to you. Also, this code worked for us because we never knew when an external source was going to edit a table. Setting the interval to a correct number of milliseconds would be a much more efficient way of doing this, as pointed out by md5sum.
这个答案可能有点左,但我们经常使用 CruiseControl.NET 来处理我们的一些问题预定的任务。它并不适合所有人,但如果这是一项您想要每天晚上运行的大型工作,并且其他代码/结果依赖于它,那么它是一个不错的选择。您可以安排它在任何时候运行,如果它工作/失败,您会收到电子邮件。如果不起作用,请运行其他脚本/代码,并在开始之前和之后清理所需的文件。
并不是所有情况的完美解决方案,但对于那些需要它的人来说它非常强大。我们将它用于一些大数据处理作业,如果它成功/失败,它会向我们所有人发送一封电子邮件,如果第一次失败,它甚至会在 30 分钟后重试。它给你一种美好的模糊感觉:)
This answer might be a bit left field, but we often use CruiseControl.NET for some of our scheduled tasks. It's not perfect for them all, but if it's a big job that you want to run every night and other code/outcomes depend on it then it's a good choice. You can schedule it to run whenever, get emails if it worked/failed. Run other scripts/code if it did not work, clean up files you need before you start and after.
Not the perfect solution to all situation, but it is damn powerful for those that call for it. We use it for some of our big data processing jobs, and it sends us all an email if it worked/failed and will even try again 30 minutes later if it failed the first time. It gives you a nice fuzzy feeling :)
Windows 任务计划程序(如 klausbyskov 建议)或 SQL Server 作业。
编辑:
或者,如果您想要动态分配的时间,您可以创建一个 Windows 服务,每 10 分钟轮询一次并在所需的时间执行一些操作。
Windows task scheduler (as suggested by klausbyskov) or a SQL Server job.
EDIT:
Or if you want a dyanically assigned time, you could create a windows service that polls every 10 minutes and performs some action at the desired time(s).
用 C# 创建 Windows 服务器 相当简单,并且可以做到这一点。只需确保您已经了解了安全性和日志记录,因为很难判断它正在(或未)运行时发生了什么。
Creating a windows server in C# is fairly trivial and could do this. Just make sure you've got the security and logging figured out because it can be pretty hard to tell what's going on while it is (or isn't) running.
使用System.Threading.Timer。
我在 ASP.NET 应用程序中生成一个线程来执行计划任务。
Use System.Threading.Timer.
I'm spawning a thread in an ASP.NET application to do scheduled tasks.
这取决于您可以使用什么。
如果您使用的是 Linux/Unix/Mac OS,您最好的选择是使用 cron 作业X,Windows 上的任务计划程序,或较新版本的 Mac OS X 上的 launchd
。如果想要在应用程序内执行此操作,您需要一个循环来定期检查时间并在 03:00 时触发事件,但这并不理想。
It depends on what you have available to you.
Your best bet is to use a cron job, if you are on Linux/Unix/Mac OS X, a task scheduler on Windows, or launchd on newer versions of Mac OS X.
If you want to do this from within an application, you would need a loop that checks the time on a regular basis and fires off the event if it is 03:00, but this isn't ideal.
该程序可以通过命令行运行吗?如果是这样,创建一个 foo.bat 文件,并调用你的程序命令行(非常简单)。
然后使用 任务计划程序 在每天凌晨 3 点运行 .bat 文件。
Is the program able to run via command line? If so, create a foo.bat file, and call your program command line (very simple).
Then use Task Scheduler to run the .bat file at 3 a.m. daily.