简体   繁体   English

C#如何暂停计时器?

[英]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. 我有一个C#程序,如果用户停止与程序交互,我需要停止计时器。 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. 您可以通过在.NET中使用Stopwatch类来实现此目的。 By simply stopping and starting you continue the use of the instance of the stopwatch. 只需停止并启动即可继续使用秒表实例。

Make sure to make use of using System.Diagnostics; 确保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: 要重置秒表,只需调用ResetRestart方法,如下所示:

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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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