我想创建一个新的调度程序计时器,每次使用计时器值调用方法foo(30)时会有不同的句点:
void foo(int timerValueInSeconds)
{
TimeSpan _DestructionTimer = TimeSpan.FromSeconds(timerValueInSeconds);
DateTime _FullDestructionTimer = DateTime.Now + _DestructionTimer;
DispatcherTimer _DispatcherTimer = new DispatcherTimer();
_DispatcherTimer.Interval = TimeSpan.FromMilliseconds(1000);
_DispatcherTimer.Tick += new EventHandler((sender, e) => DispatcherTimer_Tick(sender, e, _FullDestructionTimer, _NewMessage, _DispatcherTimer));
_DispatcherTimer.Start();
}
private void DispatcherTimer_Tick(object sender, EventArgs e, DateTime timer, Message _message, DispatcherTimer _dispatcherTimer)
{
var remaining = timer - DateTime.Now;
int remainingSeconds = (int)remaining.TotalSeconds;
//update timer in view
_message.Timer.ValueInSeconds = remainingSeconds;
//stop the timer if reach to zero
if (remaining.TotalSeconds <= 0)
{
_dispatcherTimer.Stop();
//remove message from list
}
}
当我调用foo(30)三次时,我希望每个计时器应该在一个不同的并行线程中运行,具有指定的周期,在每个计时器独立完成后更新视图。
我调用foo(30),然后等待(例如)3秒,然后再次调用foo(30)。定时器根据先前的倒计时器值(27秒)进行倒计时,然后以相同的值倒计时。