简体   繁体   English

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

[英]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.

相关问题 如何防止定时器在第一次启动时启动延迟? - how to prevent timer to start the delay in the first time its start? 如何延迟计时器正确启动? - How to do a delay of timer start properly? 如何根据当前日期(以秒为单位)以秒为单位获取日期开始日期 - How to get day start date in seconds based on current date in seconds 如何使用Timer.Timer(或)如何将循环中的一些代码从当前定时器循环到给定时间? - how to loop through piece of code from current timer to given time using Timer.Timer (or) How to add Timer to loop? 如何在特定时间的特定时间启动计时器? - How to start a timer at a specific time for a specific time? 当用户从 DatePicker 中选择日期时,如何在特定日期启动计时器? - How to start timer on specific date when user picks a date from DatePicker? 窗口服务:如何在特定时间启动计时器 - Window service : How to start a Timer at Particular Time 如何在C#中以正确的时间检索当前周的开始日期和结束日期? - How to retrieve start date and end date with correct time for current week in C#? 从开始日期开始查找结束日期并消除延迟 - finding end date from start date with removing a delay 如何通过DB设置倒数计时器的开始时间和结束时间? C#ASP.NET - How to set a countdown timer with a start time and an end time from the DB? C# ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM