【问题标题】:Timer Events, Increments, and Resets in C#C# 中的定时器事件、增量和重置
【发布时间】:2010-03-09 20:15:35
【问题描述】:

在我的 C# 应用程序中使用了一个计时器来确定预期的事件是否及时发生。这就是我目前尝试这样做的方式:

// At some point in the application where the triggering event has just occured.
   // Now, the expected event should happen within the next second.
   timeout = false;
   timer1.Interval = 1000; // Set timeout for 1 second.
   timer1.Start();

timer1_Tick(object sender, EventArgs e)
{
   timeout = true;
}

// At some point in the application where the expected event has occured.
   timer1.Stop();

// At a later point in the application where the timeout is
// checked, before procedding.
   if ( timeout )
   {
      // Do something.
   }

现在,我想知道的是,当调用 Start()Stop() 成员方法时,是否会导致计时器计数重置?我正在使用 Microsoft Visual C# 2008 Express Edition。谢谢。

【问题讨论】:

    标签: c# winforms count timer


    【解决方案1】:

    当您调用 Stop() 时,它会有效地将计时器从链接页面重置回 0:

    禁用后调用开始 通过调用 Stop 的 Timer 将导致 重启中断的定时器 间隔。如果您的计时器设置为 5000 毫秒的间隔,而你 在大约 3000 毫秒时调用 Stop, 调用 Start 将导致 Timer 等待 5000 毫秒之前 引发 Tick 事件。

    【讨论】:

    • 谢谢。我确实看过 MSDN 文章,但我一定错过了这个花絮。
    【解决方案2】:

    来自MSDN

    在您通过调用 Stop 禁用 Timer 后调用 Start 将导致 Timer 重新启动中断的间隔。如果您的 Timer 设置为 5000 毫秒间隔,并且您在大约 3000 毫秒时调用 Stop,则调用 Start 将导致 Timer 在引发 Tick 事件之前等待 5000 毫秒。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 2022-08-19
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 2011-02-24
      相关资源
      最近更新 更多