How to pause a timer in C #?

I have a timer in my code with an interval of 10 seconds.

When the time runs out, I will do some checking. It may take more than 10 seconds to check and some update tasks.

However, if I did not stop the timer, it seems that the check will be performed every 10 seconds. If I call stop() , the timer cannot be started again. eg:

  protected void timer_elapsed(object sender, EventArgs args) { __timer.Stop(); //Some checking here more than 10s __timer.Start(); } 

I just want another 10 seconds to be checked again before the check is completed.

Can anyone help?

+4
source share
1 answer

Assuming this is something like winforms or WPF, this should work. Are you sure you are not getting an exception or anything else?

If this is a website, all bets are disabled; -p Perhaps you should clarify if it is System.Threading.Timer , System.Timers.Timer or System.Windows.Forms.Timer or something else.

+5
source

Source: https://habr.com/ru/post/1309072/


All Articles