关于计时器和可变间隔的问题

发布于 2024-09-06 01:29:58 字数 816 浏览 1 评论 0原文

这个计时器的目的是在午夜时执行一些代码。 所以基本上,第一个间隔将是从现在到午夜之间,之后的所有间隔都是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

书间行客 2024-09-13 01:29:58

Interval 属性是计时器调用之间的毫秒数。

要在午夜运行计时器,您需要将每个 Elapsed 事件中的 Interval 更改为 (int)(DateTime.Today.AddDays(1) - DateTime。现在).TotalMilliseconds

要访问 Elapsed 处理程序内的计时器,您需要将计时器存储在类中的一个字段中,如下所示:

System.Timers.Timer MyTimer
private void Form1_Load(object sender, EventArgs e)
{
    MyTimer = new System.Timers.Timer();
    //...
}

请注意,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 each Elapsed 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:

System.Timers.Timer MyTimer
private void Form1_Load(object sender, EventArgs e)
{
    MyTimer = new System.Timers.Timer();
    //...
}

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 call BeginInvoke.

蘑菇王子 2024-09-13 01:29:58

第一次运行后,您必须更新间隔,从第二天的午夜减去当前时间并分配给间隔;我为我的一个批处理过程执行此操作,效果很好。

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.

小忆控 2024-09-13 01:29:58

让你的计时器每小时触发一次,然后只在午夜进行实际工作。我还建议使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文