简体   繁体   中英

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. 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; This will prevent the timer ticks from occurring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM