C# 如何延迟计时器运行或根据当前日期时间启动计时器

C# 如何延迟计时器运行或根据当前日期时间启动计时器,c#,multithreading,C#,Multithreading,我有一个控制台应用程序作为一个应用程序的演示,它打印“hello”,基于它预期提醒用户的时间跨度。当它还没有时间跨度时,我想延迟应用程序打印hello,并在时间到时继续 public static async void timeCounter(int delae) { //This is suppose to cause a delay but it instead initiate the //TimerOperation_Tick method.

我有一个控制台应用程序作为一个应用程序的演示,它打印“hello”,基于它预期提醒用户的时间跨度。当它还没有时间跨度时,我想延迟应用程序打印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();

          }
        }

    }
公共静态异步无效计时器(int-delae)
{
//假设这会导致延迟,但它会启动
//时间限制法。
等待任务。延迟(delae);
//计时器倒计时
System.Timers.Timer Timer=新的System.Timers.Timer();
timer.Interval=1000;//1秒
timer.appead+=新的ElapsedEventHandler(TimerOperation_Tick);
timer.Start();
if(obj.SubmissionCheck==true)
{
timer.Stop();
}
}
///事件订阅服务器
私有静态无效时间限制(对象e,ElapsedEventArgs args)
{
如果(时间范围!=0)
{
Console.WriteLine(“hi”+时间段);
时间框架--;
如果(时间范围<1)
{
obj.SubmissionCheck=真;
nt.移除(obj);
开始通知();
}
}
}

尝试设置
timer.Enabled=false这将阻止计时器滴答声的发生。

我有点困惑,但是设置timer.Enabled=false应该可以阻止滴答声的发生,我相信。你不应该暂停计时器的运行。谢谢,@beardedmogul,成功了。必须公开声明计时器对象才能在计时器事件订阅服务器中使用它。