开发者

How to set an automatic restarting of a Windows Service application, C#?

I need to create some windows service which will execute every some often, every hour. The question is - which timer control should I use: System.Timers.Timer or System.Threading.Timer one? I am asking because I have been reading through the forum and I am get开发者_开发技巧ting a few mixed reviews on both. Also, the explanations are kind of hard to follow as I am pretty new to programming. Thank you.


Read @Jon Skeet's Timers write-up: http://www.yoda.arachsys.com/csharp/threads/timers.shtml


I use System.Timers.Timer in my Windows services. It's easy to control and hook up. VS 2010's Intellisense even creates the elapsed event handler as you type "myTimer.Elapsed += ..."

You can also stop the timer at the beginning of your event handler and restart it at the end if you don't want the timer firing before the event handler is finished on long-running processing. For example:

System.Timers.Timer myTimer;
void Main()
{
    myTimer = new System.Timers.Timer(1000);
    myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
    myTimer.Start();
}

void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    myTimer.Stop();
    // process event
    myTimer.Start();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜