C# System.Timers.Timer 如何获取到 Elapse 的剩余时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2278525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:58:28  来源:igfitidea点击:

System.Timers.Timer How to get the time remaining until Elapse

c#timerwindows-serviceselapsedtimesystem.timers.timer

提问by Lonnie Best

Using C#, how may I get the time remaining (before the elapse event will occur) from a System.Timers.Timerobject?

使用 C#,如何从System.Timers.Timer对象中获取剩余时间(在 elapse 事件发生之前)?

In other words, let say I set the timer interval to 6 hours, but 3 hours later, I want to know how much time is remaining. How would I get the timer object to reveal this time remaining?

换句话说,假设我将计时器间隔设置为 6 小时,但是 3 小时后,我想知道还剩多少时间。我如何让计时器对象显示剩余的时间?

采纳答案by Samuel Neff

The built-in timer doesn't provide the time remaining until elapse. You'll need to create your own class which wraps a timer and exposes this info.

内置计时器不提供剩余时间,直到过去。您需要创建自己的类来包装计时器并公开此信息。

Something like this should work.

像这样的事情应该有效。

public class TimerPlus : IDisposable
{
    private readonly TimerCallback _realCallback;
    private readonly Timer _timer;
    private TimeSpan _period;
    private DateTime _next;

    public TimerPlus(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period)
    {
        _timer = new Timer(Callback, state, dueTime, period);
        _realCallback = callback;
        _period = period;
        _next = DateTime.Now.Add(dueTime);
    }

    private void Callback(object state)
    {
        _next = DateTime.Now.Add(_period);
        _realCallback(state);
    }

    public TimeSpan Period
    {
        get
        {
            return _period;
        }
    }

    public DateTime Next
    {
        get
        {
            return _next;
        }
    }

    public TimeSpan DueTime
    {
        get
        {
            return _next - DateTime.Now;
        }
    }

    public bool Change(TimeSpan dueTime, TimeSpan period)
    {
        _period = period;
        _next = DateTime.Now.Add(dueTime);
        return _timer.Change(dueTime, period);
    }

    public void Dispose()
    {
        _timer.Dispose();
    }
}

回答by anaconda

I guess the best method is to hold the start time in a variable and then calculate the elapsed time as

我想最好的方法是将开始时间保存在一个变量中,然后将经过的时间计算为

TimeSpan t = DateTime.Now - StartTime;

回答by Mike

I am aware, that the topic is more than 3 years old. However I came across it while tackling exactly the same problem.

我知道,这个话题已经超过 3 年了。但是,我在解决完全相同的问题时遇到了它。

Inspired by Samuel Neff, I came up with a WinForms-less solution by extending the standard System.Timers.Timer class:

受 Samuel Neff 的启发,我通过扩展标准 System.Timers.Timer 类想出了一个无 WinForms 的解决方案:

    public class TimerPlus : System.Timers.Timer
    {
        private DateTime m_dueTime;

        public TimerPlus() : base()
        {
            this.Elapsed += this.ElapsedAction;
        }

        protected new void Dispose()
        {
            this.Elapsed -= this.ElapsedAction;
            base.Dispose();
        }

        public double TimeLeft
        {
            get
            {
                return (this.m_dueTime - DateTime.Now).TotalMilliseconds;
            }
        }

        public new void Start()
        {
            this.m_dueTime = DateTime.Now.AddMilliseconds(this.Interval);
            base.Start();
        }

        private void ElapsedAction(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (this.AutoReset)
            {
                this.m_dueTime = DateTime.Now.AddMilliseconds(this.Interval);
            }
        }
    }

I hope it helps.

我希望它有帮助。