Trigger a Timer Event at the Specific time of the Day in a Windows Service

I have used timer many times in Windows Form Application. It’s a simple control that is easy to use and implement. Lately, I was working on a Windows Service where there was a requirement of implementing a periodic job. The timer available in Windows Form is located in System.Windows.Form namespace which is definitely not available (by default) in a Windows Service. I tried to dig into the matter and I came to know that there are three types of timers available in .NET:

  • A server-based timer, which you can add to the Toolbox
  • A Windows-based timer, which is always in the Toolbox
  • A thread timer, which is available programmatically

You can find the details about these on this MSDN Link. I shall not be going into the details of implementing each and every timer, I will just try to focus on the subject. By the way, it is very much confusing for a general user to choose the best timer. Each timer is more suitable in certain circumstances. Here is an old article (since 2004) from MSDN Magazine that will give you a great insight on these timers and their differences and usage. The source code is also available there. I would highly recommend checking the page.

After a lot of tests and search I got this Stack Overflow page which was close to my requirements.

[sourcecode language=”csharp”]
private System.Threading.Timer myTimer;
private void SetTimerValue ()
{
// trigger the event at 7 AM. For 7 PM use 19 i.e. 24 hour format
DateTime requiredTime = DateTime.Today.AddHours(7).AddMinutes(00);
DateTime currentTime = DateTime.Now;
if (currentTime > requiredTime)
{
requiredTime = requiredTime.AddDays(1);
}
// interval between the timer events is set 10 min
TimeSpan periodTS = DateTime.Now.AddMinutes(10) – DateTime.Now;
myTimer = new System.Threading.Timer(new TimerCallback(TimerAction), null, (requiredTime – DateTime.Now), periodTS );
}

private void TimerAction (object e)
{
// do some work
}
[/sourcecode]

This piece of code completed the initial requirement to trigger the timer at the specific time of the day but with the passage of time, the event trigger time started decreasing by 1 second after 10 hours, with the interval of 10 min. If the interval is decreased the error will propagate upward much quickly.

I revised the code and made some modifications:

[sourcecode language=”csharp”]
private System.Threading.Timer myTimer;
private void SetTimerValue ()
{
// trigger the event at 7 AM. For 7 PM use 19 i.e. 24 hour format
DateTime requiredTime = DateTime.Today.AddHours(7).AddMinutes(00);
if (DateTime.Now > requiredTime)
{
requiredTime = requiredTime.AddDays(1);
}

// initialize timer only, do not specify the start time or the interval
myTimer = new System.Threading.Timer(new TimerCallback(TimerAction));
// first parameter is the start time and the second parameter is the interval
// Timeout.Infinite means do not repeat the interval, only start the timer
myTimer.Change((int)(requiredTime – DateTime.Now).TotalMilliseconds, Timeout.Infinite);
}

private void TimerAction(object e)
{
// do some work
// now, call the set timer method to reset its next call time
SetTimerValue();
}
[/sourcecode]

5 thoughts on “Trigger a Timer Event at the Specific time of the Day in a Windows Service”

  1. Simply wish to say your article is as surprising. The clearness
    in your submit is just cool and that i could suppose you’re an expert in this subject. Fine along with your permission allow me to grab your feed to keep updated with coming near near post. Thank you one million and please keep up the rewarding work.

    Reply
  2. I there, I run into your article while looking for something to solve a similar requirement on my application. I should mention I am new on C# and when trying your code I got an error on TimerCallback, Timeout.Infinite. It seams I am missing a reference to a library, could you please help suggesting what could I be missing?

    Reply

Leave a Reply to Bilal Cancel reply

%d bloggers like this: