【问题标题】:WinForms: How can I use a timer to run code at the end of it's interval but have the interval reset through it's countdownWinForms:如何使用计时器在其间隔结束时运行代码但通过倒计时重置间隔
【发布时间】:2017-10-21 13:22:06
【问题描述】:

我正在尝试将日志输出到表单,但我希望在可见 5 秒后清除。但如果我在这 5 秒倒计时期间输出另一个日志,我希望将其重置为 5 秒。

OutputLog("消息", "标题");
5..4..3..2..1..
清除日志();

但我可能有这种情况;

OutputLog("消息", "标题");
5..4..
OutputLog("message2", "title2");
5..4..3..2..
OutputLog("message3", "title3");
5..4..3..2..1..
清除日志();

同时不停止当前线程。

目前我有以下,但这是一个恒定的 5 秒,因此例如日志可能只出现 0.5 秒;

Task.Run(async delegate
{
    await Task.Delay(5000);
    lblLogOutput.Text = "";
});

【问题讨论】:

  • 只需使用间隔为 5000 的计时器。在 OutputLog 中停止并启动计时器。当它滴答作响时,您就知道已经过去了 5 秒。
  • 我会检查一下,谢谢。

标签: c# winforms asynchronous timer


【解决方案1】:

您可以使用Stop 函数紧跟Start 函数来“重新启动”计时器。

private void restartTimer() 
{
  timer1.Stop();
  timer1.Start();
}

【讨论】:

  • 您可能应该提及您正在谈论的什么类型的“计时器”。目前,OP 没有使用一个
猜你喜欢
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
  • 2013-08-03
  • 2021-12-18
  • 1970-01-01
相关资源
最近更新 更多