C # How to pause a timer?

I have a C # program in which I need a timer to stop if the user stops interacting with the program. What needs to be done is to pause and then restart when the user becomes active again. I did some research and found that there are such commands as:

timer.Stop(); 

and

 timer.Start(); 

But I was wondering if this was:

 timer.Pause(); 

And then, when the user is activated again, he raises the place where he stopped, and does not restart. If anyone can help, that would be very appreciated! Thanks,

Micah

+5
source share
2 answers

You achieve this using the Stopwatch class in .NET. Just stopping and starting, you continue to use the stopwatch instance.

Be sure to use using System.Diagnostics;

 var timer = new Stopwatch(); timer.Start(); timer.Stop(); Console.WriteLine(timer.Elapsed); timer.Start(); //Continues the timer from the previously stopped time timer.Stop(); Console.WriteLine(timer.Elapsed); 

To reset the stopwatch, simply call the Reset or Restart methods, as shown below:

 timer.Reset(); timer.Restart(); 
+10
source

There is no pause, because it is easy to make an equivalent. You can simply stop the timer instead of pausing it, and then when you need to restart it, you just need to specify the remaining time. It may be difficult, or it may be simple; it depends on what you use the timer. The fact that what you do depends on what you use the timer for, probably the reason that the pause does not exist.

Perhaps you are using a timer to do something that repeats in a regular period of time, or you can use a timer to count time to a specific time. If you do something (for example, every second) repeatedly, then your requirements can be restarted at the beginning of this period (second) or on that part of this period. What happens if a pause lasts longer? Usually, missed events are ignored, but it depends on the requirements.

Therefore, I am trying to say that you need to determine your requirements. Then, if you need help, specify what you need.

+3
source

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


All Articles