jump to navigation

Trigger a Timer Event at the Specific time of the Day in a Windows Service June 7, 2012

Posted by Bilal in .NET, C#, Programming.
Tags: , , , , , , , , , ,
trackback

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.

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
}

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:

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();
}
Advertisements

Comments»

1. how to reset admin password windows 7 - February 6, 2013

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.

2. Luis - May 29, 2013

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?

3. Luis - May 29, 2013

Never mind … it was missing using System.Threading

4. Bilal - May 30, 2013

This is a very typical requirement and not a usual case. Its good the you made it

glad to know that it helped you

5. MArtin Puls - August 27, 2013

Somehow the timer triggers a second before the time and then again at the appointed time.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: