关于计时器和可变间隔的问题
这个计时器的目的是在午夜时执行一些代码。 所以基本上,第一个间隔将是从现在到午夜之间,之后的所有间隔都是 24 小时。
现在一切都很好,但我想知道这个计时器业务是如何运作的。每次计时器重置时,MyTimer.Interval 是否都会重新计算?
System.Timers.Timer MyTimer = new System.Timers.Timer();
MyTimer.Elapsed += new ElapsedEventHandler(TriggeredMethod);
MyTimer.Interval = [pseudocode]Time between now and midnight[/pseudocode];
MyTimer.Start();
编辑:
我在设置 TriggerMethod 内部的时间间隔时遇到问题。我应该在哪里/如何启动计时器,这样我就不会收到任何上下文错误?
private void Form1_Load(object sender, EventArgs e)
{
System.Timers.Timer MyTimer = new System.Timers.Timer();
MyTimer.Elapsed += new ElapsedEventHandler(TriggeredMethod);
MyTimer.Start();
}
private void TriggerMethod(object source, ElapsedEventArgs e)
{
MyTimer.Interval = [pseudocode]Time between now and midnight[/pseudocode];
}
The point of this timer is to execute some code whenever it's midnight.
So basically, the first interval will be between Now and midnight and all intervals following that will be 24hours.
Now that's all well and good, but I was wondering how this timer business works. Is the MyTimer.Interval recalculated each time the timer resets?
System.Timers.Timer MyTimer = new System.Timers.Timer();
MyTimer.Elapsed += new ElapsedEventHandler(TriggeredMethod);
MyTimer.Interval = [pseudocode]Time between now and midnight[/pseudocode];
MyTimer.Start();
EDIT:
I'm having trouble setting the Interval inside of my TriggerMethod. Where/how should I initiate the Timer so I don't get any context errors?
private void Form1_Load(object sender, EventArgs e)
{
System.Timers.Timer MyTimer = new System.Timers.Timer();
MyTimer.Elapsed += new ElapsedEventHandler(TriggeredMethod);
MyTimer.Start();
}
private void TriggerMethod(object source, ElapsedEventArgs e)
{
MyTimer.Interval = [pseudocode]Time between now and midnight[/pseudocode];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Interval
属性是计时器调用之间的毫秒数。要在午夜运行计时器,您需要将每个
Elapsed
事件中的Interval
更改为(int)(DateTime.Today.AddDays(1) - DateTime。现在).TotalMilliseconds
。要访问
Elapsed
处理程序内的计时器,您需要将计时器存储在类中的一个字段中,如下所示:请注意,
System.Timers.Timer< /code> 不会在 UI 线程上触发。
因此,您无法在
Elapsed
处理程序内操作表单。如果需要操作表单,可以切换到 System.Windows.Forms.Timer(不太准确)或调用 BeginInvoke。
The
Interval
property is the number of milliseconds between timer invocations.To run the timer at midnight, you would need to change
Interval
in eachElapsed
event to(int)(DateTime.Today.AddDays(1) - DateTime.Now).TotalMilliseconds
.To access the timer inside the
Elapsed
handler, you'll need to store the timer in a field in your class, like this:Note, by the way, that
System.Timers.Timer
will not fire on the UI thread.Therefore, you cannot manipulate the form inside the
Elapsed
handler.If you need to manipulate the form, you can either switch to
System.Windows.Forms.Timer
(which is less accurate) or callBeginInvoke
.第一次运行后,您必须更新间隔,从第二天的午夜减去当前时间并分配给间隔;我为我的一个批处理过程执行此操作,效果很好。
HTH。
after the first run, you would have to update the interval, subtracting the current time from midnight the following day and assigning to the interval; I do this for one of my batch processes, and it works well.
HTH.
让你的计时器每小时触发一次,然后只在午夜进行实际工作。我还建议使用 BeginInvoke 将实际的 UI 交互转移到 GUI 线程上。
Make your timer fire once per hour, and then only do the actual work at midnight. And I also recommend using BeginInvoke to move the actual UI interaction onto the GUI thread.