如何使用.NET Timer类在特定时间触发事件?
我希望在我的应用程序中触发一个事件,该事件在一天中的某个时间(例如下午 4:00)连续运行。我考虑每秒运行一次计时器,当时间等于下午 4:00 时运行该事件。那行得通。但我想知道是否有办法只在下午 4:00 回调一次,而不必继续检查。
I would like to have an event triggered in my app which runs continuously during the day at a certain time, say at 4:00pm. I thought about running the timer every second and when the time is equal to 4:00pm run the event. That works. But I'm wondering if there's a way to just get the callback once at 4:00pm and not having to keep checking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
怎么样,使用
System.Threading。定时器
类?请注意,如果您使用 System.Threading.Timer,则由 TimerCallback 指定的回调将在线程池(非 UI)线程上执行,因此如果您如果计划在 4:00 对 UI 执行某些操作,则必须适当地编组代码(例如,在 Windows 窗体应用程序中使用
Control.Invoke
或Dispatcher.Invoke
代码 > 在 WPF 应用程序中)。How about something like this, using the
System.Threading.Timer
class?Note that if you use a
System.Threading.Timer
, the callback specified byTimerCallback
will be executed on a thread pool (non-UI) thread—so if you're planning on doing something with your UI at 4:00, you'll have to marshal the code appropriately (e.g., usingControl.Invoke
in a Windows Forms app, orDispatcher.Invoke
in a WPF app).从 .NET 4.5 开始,有一个非常干净的解决方案:
这是一个没有异步/等待的解决方案:
应该注意的是,由于 int32 最大值,这仅适用于大约 24 天,这对于您的情况来说已经足够了,但值得注意。
Starting with .NET 4.5 there's a really clean solution:
Here's a solution without async/await:
It should be noted that this only works for about 24 days out because of int32 max value, which is plenty for your case, but worth noting.
以 VoteCoffees 为主导,这是一个紧凑的基于事件的解决方案:
消费者:`
Taking VoteCoffees lead, here is a compact event based solution:
Consumer:`
您可以使用Windows 上的任务计划程序有关详细信息,请参阅每日触发示例。
或者如果您想自己编写,请使用以下代码:
You can use Task Sceduler on windows See daily trigger example for detail.
or use bellow code if you want wrote it yourself:
.NET 有很多计时器类,但它们都需要相对于当前时间的时间跨度。对于相对时间,在启动计时器之前需要考虑很多事情并在计时器运行时进行监控。
操作系统非常适合处理这种复杂性。 .NET 上运行的应用程序代码则不然。
对于 Windows,NuGet 包 AbsoluteTimer 包装在绝对时间到期的操作系统计时器。
.NET has lots of timer classes, but they all take time spans relative to the current time. With a relative time, there are lots of things to consider before you start your timer and to monitor for while your timer is running.
The operating system is in a great place to handle this complexity. Application code running on .NET is not.
For Windows, the NuGet package AbsoluteTimer wraps an operating system timer that expires at an absolute time.
我每天早上7点都是这样做的
I did this way to fire 7am every morning
任务调度程序是一个更好的选择,它可以很容易地在 C# 中使用,http://taskscheduler.codeplex.com/
Task scheduler is a better option, and it can be easily used in C#, http://taskscheduler.codeplex.com/
呼应 Dan 的解决方案,使用 Timercallback 是一个快速而简洁的解决方案。
在要安排要运行的任务或子例程的方法内部,请使用以下内容:
使用“Timeout.Infinite”将确保回调仅在 400 毫秒后执行一次。我正在使用VB.Net。
Echo to Dan's solution, using Timercallback is a quick and neat solution.
Inside the method you want to schedule a task or subroutine to be run, use the following:
use of 'Timeout.Infinite' will ensure the callback will be executed only once after 400ms. I am using VB.Net.
这个解决方案怎么样?
How about this solution?