简体   繁体   中英

C# timer synchronized with Datetime.Now

In C# how to create a timer that synchronize with tick of system DateTime. custom timer should tick whenever the seconds change for DateTime.Now

You will find if you check some of the previous questions on SO, you are going to have a very hard time getting accuracy with your timer. Here is a small sample of previous questions:

Here is another previous question which may provide you some help.

What would be your desired precision ?

If you say that 1ms is precise enough (or 10, or 100), you could always do a loop which compares the current time and one saved from the previous iteration, and start a thread when the seconds value of the current time value changes...

您可以使用一些时间表库,例如Quartz.net,它提供了示例并且易于使用: http : //quartznet.sourceforge.net/

This code uses a System.Threading.Timer. It will look at the millisecond-offset the trigger is called. If the error is outside the accepted range, it will re-adjust the timer and it will correct the interval by averaging the error per tick.

class Program {
    const int MAX_ERROR_IN_MILLISECONDS = 20;
    const int ONE_SECOND = 1000;
    const int HALF_SECOND = ONE_SECOND / 2;

    private static System.Threading.Timer timer;

    static void Main(string[] args) {

        timer = new System.Threading.Timer(Tick);

        timer.Change(ONE_SECOND - DateTime.Now.Millisecond, ONE_SECOND);
        Console.ReadLine();
    }

    private static int ticksSynced = 0;
    private static int currInterval = ONE_SECOND;
    private static void Tick(object s) {
        var ms = DateTime.UtcNow.Millisecond;
        var diff = ms > HALF_SECOND ? ms - ONE_SECOND : ms;
        if (Math.Abs(diff) < MAX_ERROR_IN_MILLISECONDS) {
            // still synced
            ticksSynced++;
        } else {
            // Calculate new interval
            currInterval -= diff / ticksSynced;
            timer.Change(ONE_SECOND - ms, currInterval);
            Console.WriteLine("New interval: {0}; diff: {1}; ticks: {2}", currInterval, diff, ticksSynced);
            ticksSynced = 0;
        }
        Console.WriteLine(ms);
    }
}

As you can see, you cannot trigger exactly on the second change in Windows. You can try to get close.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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