gpt4 book ai didi

c# - 2个计时器,一个落后另一个n秒

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:44 25 4
gpt4 key购买 nike

这是我的问题:我有一个间隔为 5 分钟的计时器。我想要的是通知计时器只剩下 30 秒,这样我就可以启用/禁用某些控件(c# 中的 winforms)。我假设我为此需要 2 个计时器,也许是一个新的 MyTimer 类,它将这 2 个计时器作为数据成员。第一个计时器为 5 分钟,第二个为 4.5 分钟。第二个计时器会引发一个我会听的事件。有人对此有更好的想法吗?感谢您的帮助。

最佳答案

正如 Floris Velleman 上面提到的,最好使用 1 个以最小时基递增的计时器,例如 30 秒甚至 1 秒。然后使用一个计数器和一些会在特定时间使用react的条件语句。拥有 2 个计时器需要在两者之间进行同步,这可能会导致一些问题。

例如,间隔为 1 秒,您可以这样做:

int TimerInterval = 60 * 5; // 5 minutes
int NotifyTime = 30; // 30 seconds
int counter = 0;

// timer interval is set to 1 second
private void callback_MyTimer(object sender, EventArgs e)
{
counter--;

if (counter == NotifyTime)
{
// notify that 30 seconds are left...
}

// restart the counter for 5 minutes
if (counter <= 0)
{
counter = TimerInterval;
}
}

关于c# - 2个计时器,一个落后另一个n秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20639707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com