簡體   English   中英

WinForms:如何使用計時器在間隔時間結束時運行代碼,但通過倒計時重置間隔

[英]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 countdown

我正在嘗試將日志輸出到表單,但是我希望在5秒鍾的可見性后清除日志。 但是,如果我在這5秒鍾的倒數計時期間輸出另一個日志,則希望將其重置為5秒。

OutputLog(“ message”,“ title”);
5..4..3..2..1 ..
清除日志();

但是我可能有這種情況。

OutputLog(“ message”,“ title”);
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 = "";
});

您可以使用“ Stop功能,然后立即使用“啟動”功能來“重新啟動”計時器。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM