0
我有控制檯應用程序正在使用作爲演示應用程序,它打印「hello」,基於它預期提醒用戶的時間。當它還沒有時間跨度時,我想延遲應用程序打印你好,並恢復時間。如何延遲計時器運行或基於當前日期時間啓動它
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();
}
}
}
我有點困惑,但設置timer.Enabled = false應該防止蜱發生,我相信。您不應該停止定時器操作。 – beardedmogul
謝謝,@beardedmogul,做到了。只需公開聲明定時器對象以在定時器事件訂閱服務器中使用它。 –