[英]How to delay a timer from running or start it based on current date time
I have console application am using as demo to an App, it prints "hello", based on the timespan its expected to alert the user. 我有控制台应用程序正在用作应用程序的演示,它会根据其警告用户的时间跨度打印“ hello”。 when its not yet the timespan, i want to delay the app from printing hello and resume when its time.
当尚未到时时,我想延迟应用程序打印问候并在其时间恢复。
public static async void timeCounter(int delae)
{
//This is suppose to cause a delay but it instead initiate the
//TimerOperation_Tick method.
await Task.Delay(delae);
// timer countdown
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 1000; // 1 second
timer.Elapsed += new ElapsedEventHandler(TimerOperation_Tick);
timer.Start();
if (obj.SubmissionCheck == true)
{
timer.Stop();
}
}
/// the event subscriber
private static void TimerOperation_Tick(object e, ElapsedEventArgs args)
{
if (timeFrame != 0)
{
Console.WriteLine("hi" + timeFrame);
timeFrame --;
if (timeFrame < 1)
{
obj.SubmissionCheck = true;
nt.Remove(obj);
startNotification();
}
}
}
Try setting timer.Enabled = false;
尝试设置
timer.Enabled = false;
This will prevent the timer ticks from occurring. 这将防止计时器滴答声的发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.