简体   繁体   中英

C# How to pause a timer?

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

timer.Stop(); 

and

timer.Start();

But I was wondering if there was like a:

timer.Pause();

And then when the user becomes active again, it picks up where it left off, and doesn't restart. If anyone can help, it would be much appreciated! Thanks,

Micah

You achieve this by using the Stopwatch class in .NET. By simply stopping and starting you continue the use of the instance of the stopwatch.

Make sure to make use of 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, just call the Reset or Restart methods, like below:

timer.Reset();
timer.Restart();

There is not a pause because it is easy to do the equivalent. You can just stop the timer instead of pausing it, then when you need to restart it you just need to specify the amount of time remaining. It might be complicated or it might be simple; it depends on what you are using the timer to do. The fact that what you do depends on what you are using the timer for is probably the reason a pause does not exist.

You might be using the timer to do something repetitively at a regular time period or you might be using the timer to count down to a specific time. If you are doing something (such as every second) repetitively then your requirements might be to restart at the beginning of that time period (a second) or at a portion of that period. What happens if the pause is for more than the time period? Usually the missed events would be ignored but that depends on requirements.

So I am trying to say that you need to determine your requirements. Then if you need help then clarify what you need.

I have created this class for this situation:

public class PausableTimer : Timer
{
    public double RemainingAfterPause { get; private set; }

    private readonly Stopwatch _stopwatch;
    private readonly double _initialInterval;
    private bool _resumed;

    public PausableTimer(double interval) : base(interval)
    {
        _initialInterval = interval;
        Elapsed += OnElapsed;
        _stopwatch = new Stopwatch();
    }

    public new void Start()
    {
        ResetStopwatch();
        base.Start();
    }

    private void OnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
    {
        if (_resumed)
        {
            _resumed = false;
            Stop();
            Interval = _initialInterval;
            Start();
        }

        ResetStopwatch();
    }

    private void ResetStopwatch()
    {
        _stopwatch.Reset();
        _stopwatch.Start();
    }

    public void Pause()
    {
        Stop();
        _stopwatch.Stop();
        RemainingAfterPause = Interval - _stopwatch.Elapsed.TotalMilliseconds;
    }

    public void Resume()
    {
        _resumed = true;
        Interval = RemainingAfterPause;
        RemainingAfterPause = 0;
        Start();
    }

}

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