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

后端 未结 14 2093
-上瘾入骨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:44

    Running a bit of code to see if the minute has changed once per second should not require much CPU time, and should be acceptable.

    0 讨论(0)
  • 2020-11-27 03:46

    What about a combination of aquinas' answer and 'polling': (apologies for the mixture of languages)

    def waitForNearlyAMinute:
        secsNow = DateTime.Now.Second;
        waitFor = 55 - secsNow;
        setupTimer(waitFor, pollForMinuteEdge)
    
    def pollForMinuteEdge:
        if (DateTime.Now.Second == 0):
            print "Hello, World!";
            waitForNearlyAMinute();
        else:
            setupTimer(0.5, pollForMinuteEdge)
    
    0 讨论(0)
  • 2020-11-27 03:47

    What about Quartz.NET? I think its a good framework to do timed actions.

    0 讨论(0)
  • 2020-11-27 03:50

    How about:

    int startin = 60 - DateTime.Now.Second;
    var t = new System.Threading.Timer(o => Console.WriteLine("Hello"), 
         null, startin * 1000, 60000);
    
    0 讨论(0)
  • 2020-11-27 03:50

    Creating a Timer control that fires every 1 second (and usually does nothing but a simple check) will add negligible overhead to your application.

    Simply compare the value of Environment.TickCount or DateTime.Now to the last stored time (the previous 'minute tick'), and you should have a reasonably precise solution. The resolution of these two time values is about 15ms, which should be sufficient for your purposes.

    Do note however that the interval of the Timer control is not guaranteed to be that precise or even anywhere now, since it runs on the Windows message loop, which is tied in with the responsiveness of the UI. Never rely on it for even moderately precise timing - though it is good enough for firing repeating events where you can check the time using a more sensitive method such as one of the two given above.

    0 讨论(0)
  • 2020-11-27 03:51

    Building on the answer from aquinas which can drift and which doesn't tick exactly on the minute just within one second of the minute:

    static System.Timers.Timer t;
    
    static void Main(string[] args)
    {
        t = new System.Timers.Timer();
        t.AutoReset = false;
        t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
        t.Interval = GetInterval();
        t.Start();
        Console.ReadLine();
    }
    
    static double GetInterval()
    {
        DateTime now = DateTime.Now;
        return ((60 - now.Second) * 1000 - now.Millisecond);
    }
    
    static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine(DateTime.Now.ToString("o"));
        t.Interval = GetInterval();
        t.Start();
    }
    

    On my box this code ticks consistently within .02s of each minute:

    2010-01-15T16:42:00.0040001-05:00
    2010-01-15T16:43:00.0014318-05:00
    2010-01-15T16:44:00.0128643-05:00
    2010-01-15T16:45:00.0132961-05:00
    
    0 讨论(0)
提交回复
热议问题