Timer is very important for windows service related applications as per my knowledge. In this example, I am showing .net framework System.Timers namespace which is having timer class and related methods and properties. here ElapsedEventHandler is the delegate calling the DisplayTimeEventMethod method for every 5 seconds. Start() will start the timer.
If you enter 'q' and enter console application will exit.
output:
After discussion in linked in, timer may not be a good way to handle with services.
"calling the "worker" method from another one inside the
service - one /with/ a loop and Sleep(s) - works 100% reliably, and you
even get to catch the Exception"
If you enter 'q' and enter console application will exit.
output:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Timers; namespace TimerExample { class Program { static void Main(string[] args) { Timer myTime = new Timer(); myTime.Elapsed += new ElapsedEventHandler(DisplayTimeEventMethod); myTime.Interval = 2000; myTime.Start(); while (Console.Read() != 'q') { Console.WriteLine(DateTime.Now); } } public static void DisplayTimeEventMethod(object source, ElapsedEventArgs e) { Console.WriteLine(DateTime.Now); } } }
"calling the "worker" method from another one inside the
service - one /with/ a loop and Sleep(s) - works 100% reliably, and you
even get to catch the Exception"
Thanks Michel Posseth for improving this post quality.
Post a Comment
Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.