簡體   English   中英

獲取 System.Windows.Forms.Timer 的值?

[英]Get the value of a System.Windows.Forms.Timer?

Windows 窗體計時器有一個小問題。 這是一個非常基本的問題,但我環顧四周,似乎找不到答案(我可能值得一巴掌)。

我需要能夠獲取計時器的值,無論其經過的時間是否大於 500 毫秒的間隔。

就像是

Timer.Elapsed >= 500

Timer.Elapsed不是返回“已用時間”的屬性 - 它是您訂閱的事件 這個想法是事件每隔一段時間就會觸發一次。

目前還不清楚您是否想要一個Timer - 也許System.Diagnostics.Stopwatch真的是您所追求的?

var stopwatch = Stopwatch.StartNew();
// Do some work here

if (stopwatch.ElapsedMilliseconds >= 500)
{
    ...
}

我需要能夠獲取計時器的值,無論其經過的時間是否大於 500 毫秒的間隔。

計時器不提供允許您確定已經過去了多少時間的界面。 他們唯一能做的就是在過期時觸發一個事件。

您需要使用其他一些機制來記錄時間的流逝,例如Stopwatch類。

將 Timer 的 Interval 屬性設置為您想要觸發的毫秒數(在您的示例中為 500),並為 Tick 事件添加一個事件處理程序。

你不能用Timer做到這一點。 Elapsed是到達 0 時觸發的事件。

如果您想在事件過去時收聽,請注冊對Elapsed的收聽。 Interval是設置等待時間的成員。

請參閱此處: http : //msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.100).aspx

我寫得很快,可能有一些錯誤,但給你一個大致的想法

Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 500;
timer.Elapsed += (s,a) => {
  MyFunction();
  timer.Stop();
}
timer.Start();

基於我在此處與 David 關於StopwatchDateTime討論,我決定針對您需要獲取剩余時間的情況發布兩種方法(非常簡化),以便您可以決定哪一種更適合您:

public partial class FormWithStopwatch : Form
{
    private readonly Stopwatch sw = new Stopwatch();
    // Auxiliary member to avoid doing TimeSpan.FromMilliseconds repeatedly
    private TimeSpan timerSpan;

    public void TimerStart()
    {
        timerSpan = TimeSpan.FromMilliseconds(timer.Interval);
        timer.Start();
        sw.Restart();
    }

    public TimeSpan GetRemaining()
    {
        return timerSpan - sw.Elapsed;
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        // Do your thing
        sw.Restart();
    }
}

public partial class FormWithDateTime : Form
{
    private DateTime timerEnd;

    public void TimerStart()
    {
        timerEnd = DateTime.Now.AddMilliseconds(timer.Interval);
        timer.Start();
    }

    public TimeSpan GetRemaining()
    {
        return timerEnd - DateTime.Now;
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        // Do your thing
        timerEnd = DateTime.Now.AddMilliseconds(timer.Interval);
    }
}

老實說,我沒有看到使用Stopwatch有什么好處。 通過使用DateTime您實際上需要更少的行。 此外,后者對我來說似乎更清楚一點。

我帶來了一個我的解決方案,它有點簡單:

1 - 在啟動計時器之前,我將當前時間存儲在TotalMillisseconds (來自DateTime.Now.TimeOfDay.TotalMilliseconds ):

double _startingTime = DateTime.Now.TimeOfDay.TotalMilliseconds;

2 - 每次計時器滴答作響時,我都會再次獲取當前時間,然后我使用double變量來獲取這兩者之間的差異:

double _currentTime = DateTime.Now.TimeOfDay.TotalMilliseconds;
double _elapsed = _currentTime - _startingTime;

if(_elapsed >= 500)
{
    MessageBox.Show("As you command, master!");
    _startingTime = _currentTime;
}

if(_currentTime < _startingTime)
    _startingTime = _currentTime;

3 - 最后,因為TotalMilliseconds將返回自 00:00(12pm)以來經過的毫秒數,這意味着當它是午夜時, TotalMilliseconds將等於 0。在這種情況下,我只是檢查_currentTime是否是低於_startingTime ,如果是這樣,請將_startingTime設置為_currentTime ,以便我可以再次計算。

我希望這有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM