如何在C#中运行同步计时器?
我正在写一个应用程序,它使用计时器在屏幕上显示某个事件发生时的倒计时。我想重用计时器,因为它对应用程序中的一些事情很方便,所以我指定了我想在计时器周围加上的单词。例如,以下函数调用:如何在C#中运行同步计时器?,c#,asynchronous,timer,C#,Asynchronous,Timer,我正在写一个应用程序,它使用计时器在屏幕上显示某个事件发生时的倒计时。我想重用计时器,因为它对应用程序中的一些事情很方便,所以我指定了我想在计时器周围加上的单词。例如,以下函数调用: CountdownTimer(90, "You have ", " until the computer reboots"); 将显示: You have 1 minute 30 seconds until the computer reboots 然后倒计时 我正在使用以下代码: private vo
CountdownTimer(90, "You have ", " until the computer reboots");
将显示:
You have 1 minute 30 seconds until the computer reboots
然后倒计时
我正在使用以下代码:
private void CountdownTimer(int Duration, string Prefix, string Suffix)
{
Countdown = new DispatcherTimer();
Countdown.Tick += new EventHandler(Countdown_Tick);
Countdown.Interval = new TimeSpan(0, 0, 1);
CountdownTime = Duration;
CountdownPrefix = Prefix;
CountdownSuffix = Suffix;
Countdown.Start();
}
private void Countdown_Tick(object sender, EventArgs e)
{
CountdownTime--;
if (CountdownTime > 0)
{
int seconds = CountdownTime % 60;
int minutes = CountdownTime / 60;
Timer.Content = CountdownPrefix;
if (minutes != 0)
{
Timer.Content = Timer.Content + minutes.ToString() + @" minute";
if (minutes != 1) { Timer.Content = Timer.Content + @"s"; }
Timer.Content = Timer.Content + " ";
}
if (seconds != 0)
{
Timer.Content = Timer.Content + seconds.ToString() + @" second";
if (seconds != 1) { Timer.Content = Timer.Content + @"s"; }
}
Timer.Content = Timer.Content + CountdownSuffix;
}
else
{
Countdown.Stop();
}
}
如何使此运行同步?例如,我希望以下操作等待90秒,然后重新启动:
CountdownTimer(90, "You have ", " until the computer reboots");
ExitWindowsEx(2,0)
而目前它会立即调用重新启动
欢迎任何指点
谢谢
Ben就我个人而言,我建议在倒计时结束时进行回调-可能将
操作
作为参数,这样当它完成时就会调用它
private Action onCompleted;
private void CountdownTimer(int Duration, string Prefix, string Suffix, Action callback)
{
Countdown = new DispatcherTimer();
Countdown.Tick += new EventHandler(Countdown_Tick);
Countdown.Interval = new TimeSpan(0, 0, 1);
CountdownTime = Duration;
CountdownPrefix = Prefix;
CountdownSuffix = Suffix;
Countdown.Start();
this.onCompleted = callback;
}
...
else
{
Countdown.Stop();
Action temp = this.onCompleted; // thread-safe test for null delegates
if (temp != null)
{
temp();
}
}
然后您可以将您的用法更改为:
CountdownTimer(90, "You have ", " until the computer reboots",
() => ExitWindowsEx(2,0));
就我个人而言,我建议在倒计时结束时进行回调-可能将
操作
作为参数,这样当它完成时就会被调用
private Action onCompleted;
private void CountdownTimer(int Duration, string Prefix, string Suffix, Action callback)
{
Countdown = new DispatcherTimer();
Countdown.Tick += new EventHandler(Countdown_Tick);
Countdown.Interval = new TimeSpan(0, 0, 1);
CountdownTime = Duration;
CountdownPrefix = Prefix;
CountdownSuffix = Suffix;
Countdown.Start();
this.onCompleted = callback;
}
...
else
{
Countdown.Stop();
Action temp = this.onCompleted; // thread-safe test for null delegates
if (temp != null)
{
temp();
}
}
然后您可以将您的用法更改为:
CountdownTimer(90, "You have ", " until the computer reboots",
() => ExitWindowsEx(2,0));
您可以使用AutoResetEvent:
System.Threading.AutoResetEvent _countdownFinishedEvent
= new AutoResetEvent(false);
在倒计时结束时添加此项:
_countdownFinishedEvent.WaitOne();
并将此添加到倒计时\u勾选倒计时后的else中。停止()
:
您可以使用AutoResetEvent:
System.Threading.AutoResetEvent _countdownFinishedEvent
= new AutoResetEvent(false);
在倒计时结束时添加此项:
_countdownFinishedEvent.WaitOne();
并将此添加到倒计时\u勾选倒计时后的else中。停止()
: