e diel, 9 shtator 2007

Invoking a windows service in .net in intervals

I had a requirement recently to run my little windows service every one minute or an interval thats configurable....

And Timers was straight forward solution for me...

Event on which you need to write your code is OnElapsedTime.

private void OnElapsedTime(object source, ElapsedEventArgs e)
{
streamWriter = new StreamWriter(new FileStream(
"C:\\WindowsServiceLogger.txt", System.IO.FileMode.Append));
this.streamWriter.WriteLine(" My Service got invoked again at " +
DateTime.Now.ToString());
this.streamWriter.Flush();
this.streamWriter.Close();
Start();
}
My task for the service is written in Start() method so i can call it directly OnStart and OnElapsedTime

Please remember to enable your timer OnStart and disable OnStop.

Seems all simple and easy....and it is :)

Happy Coding

1 koment:

Jhon tha...

this is gr8........but Samuel if i want to have the timer properties configurable, wer wil i put that???