none
System.Timers.Timer: use CPU clock instead of operating system clock RRS feed

  • Question

  • Hi all,

    I have a timer built in C# that must execute a few times per day, based on a result that is returned from a stored procedure.

    The timer is something like this:

    Timer timer = new Timer();
                timer.Elapsed += this.OnTimerElapsed;
    
                DateTime executionTime = DateTime.Now.AddMinutes(10);
    
                double interval = (executionTime - DateTime.Now).TotalMilliseconds;
    
                timer.Interval = interval;

    Inside OnTimerElapsed there is logic that executes the stored procedure to return the next execution time and then updates the timer's interval.

    There are two problems I'm trying to overcome:

    1. The operating system clock suffers adjustments from time to time - since I calculate the total miliseconds now, how do I know if the clock was updated and then reflect that update on my timer?

    2. This timer does not run on weekends, meaning by this that friday afternoon it receives next Monday 7am as the next execution moment. If for some reason the operating system clock suffers a major update (such as an hour update as occurred the last weekend) the timer will execute one hour after it should.

    My question is - is there any way of specifying the CPU clock instead of the operating system/computer clock? Or is there any way of creating a dependency between the C# timer and the clock, that allows me to tell timer you will execute the 8th April 2015 8am instead of telling the timer you will execute 183243245 miliseconds from now?

    Thank you.


    Tuesday, April 7, 2015 9:05 AM

Answers

  • A much simpler way is to set a variable for "next fire time" in your application.  Set your timer to fire every second or as long as you can afford to be out in case the time changes. The longer, the less power and CPU - but the amount you could be out if the time changes and your thread is still waiting for a timer tick.

    In the timer function, read the current date/time.  If it's not yet time to fire, according to your "next fire time", or the day of week is a Saturday or Sunday - then exit the timer function before the rest of the work executes.


    Darin R.

    Friday, April 10, 2015 2:24 AM

All replies

  • Windows include a special kind of objects — “Waitable Timers”: https://msdn.microsoft.com/en-us/library/windows/desktop/ms687012. With SetWaitableTimer you can set an absolute time, then wait with WaitForSingleObject. Seems that the timer is affected by the system clock changes, as you require. However you have to code this part in C++, unless there is an equivalent in C#.


    • Edited by Viorel_MVP Tuesday, April 7, 2015 6:41 PM
    Tuesday, April 7, 2015 6:37 PM
  • A much simpler way is to set a variable for "next fire time" in your application.  Set your timer to fire every second or as long as you can afford to be out in case the time changes. The longer, the less power and CPU - but the amount you could be out if the time changes and your thread is still waiting for a timer tick.

    In the timer function, read the current date/time.  If it's not yet time to fire, according to your "next fire time", or the day of week is a Saturday or Sunday - then exit the timer function before the rest of the work executes.


    Darin R.

    Friday, April 10, 2015 2:24 AM
  • Particulary Darin's reply seems to be closer of what I want to achieve.

    It seems to me that I can't escape having something else running in parralel such as a thread or a Timer in order to check clock changes.

    What I'll do now is, based on Darin's reply/idea, try to see if it is possible to listen to computer clock changes, this would be the most suitable solution for me if possible. But I will dig into it and let know any updates.

    Thank you.

    Friday, April 10, 2015 3:51 PM
  • Hello Pakojones,

    Any update? I have marked Darins reply as answer since I think it is helpful, if you think it provides no help, please unmark it.

    Thank you for your understanding and support.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 15, 2015 9:49 AM
    Moderator