.NET, event every minute (on the minute). Is a timer the best option?

后端 未结 14 2265
-上瘾入骨i
-上瘾入骨i 2020-11-27 03:02

I want to do stuff every minute on the minute (by the clock) in a windows forms app using c#. I\'m just wondering whats the best way to go about it ?

I could use a t

14条回答
  •  囚心锁ツ
    2020-11-27 03:36

    Create a method or put this code where you want the timer to start:

     int time = 60 - DateTime.Now.Second; // Gets seconds to next minute
            refreshTimer.Interval = time * 1000;
            refreshTimer.Start();
    

    And then on your tick event set the interval to 60000:

      private void refreshTimer_Tick(object sender, EventArgs e)
        {
            refreshTimer.Interval = 60000; // Sets interval to 60 seconds
            // Insert Refresh logic
        }
    

提交回复
热议问题