Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .NET 2 Threading.Timer - time drifting

I have a dll consumed by a service. Its basic job is to run every X minutes and perform some system checks. In my dll I have a top level class that declares a System.threading.timer and a Timercallback. The constructor for the class initialises the timerCallback with my thread function. In my "Onstart" handler I initialise the timer with the timercallback and set the next time to fire and interval time. In my case its every 10 minutes. Usually in these 10 minute checks there is nothing to do but the service is forced to do something at least once every day at a set time.

My problem: I am finding that during testing, the time the daily check is carried out every day is slowly drifitng away from the desired start time of 8.30. e.g. over about 20 odd days my time has drifted from 08.30 to 08.31.35. It drifts about 4 - 6 seconds every day.

My question: does anyone know why the time is drifting like this and how can I make it stick to its allotted time?

thanks

like image 450
Keith Avatar asked Dec 08 '11 13:12

Keith


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

The time "drifts" because the timer is simply not that precise. If you need to run your code as closely as possible to a certain interval, you can do something like this:

public void MyTimerCallback(object something) {
    var now = DateTime.UtcNow;
    var shouldProbablyHaveRun = new DateTime(
        now.Year, now.Month, now.Day,
        now.Hour, now.Minute - (now.Minute % 10), 0);
    var nextRun = shouldProbablyHaveRun.AddMinutes(10.0);

    // Do stuff here!

    var diff = nextRun - DateTime.UtcNow;
    timer.Change(diff, new TimeSpan(-1));
}

...assuming you are using a System.Threading.Timer instance. Modify the example if you are using any other type of timer (there are several!).

like image 129
Anders Marzi Tornblad Avatar answered Oct 01 '22 01:10

Anders Marzi Tornblad