为什么无法在Windows服务正常使用System.Timers.Timer的或System.Windows.Forms.Timer工作 [英] Why doesn't Windows Service work properly with System.Timers.Timer or System.Windows.Forms.Timer

查看:366
本文介绍了为什么无法在Windows服务正常使用System.Timers.Timer的或System.Windows.Forms.Timer工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近被质疑写一个Windows服务。我需要定期请求一个URL,并检查其可用性。为此,我决定来初始化的OnStart定时器服务的方法和做所有的工作在 timer_Tick 事件。

I have been recently challenged with writing a Windows Service. I needed to periodically request a URL and check its availability. For this I decided to initialize a timer in OnStart method of the service and do all the work in timer_Tick event.

我的第一种方法是使用 System.Windows.Forms.Timer 勾选事件。我选择了,因为我读的教程吧。不知怎的,我不能让服务工作。它安装并启动没有问题,但它不会触发事件(我连接调试器的进程,并看到它没有被解雇)。我认为,在Windows服务可能使用窗体计时器是不是一个好主意,所以我决定改用 System.Timers.Timer的键,利用其消逝事件。这也不起作用。我尝试都提到的方法在Windows窗体应用程序,它们都工作。

My first approach was using System.Windows.Forms.Timer and its Tick event. I chose for it, because of the tutorial that I was reading. Somehow I could not make the service work. It installed and started without problems, but it would not fire the event (I attached the debugger to the process and saw that it was not fired). I thought that maybe using Forms timer in Windows service is not a good idea, so I decided to switch to System.Timers.Timer and utilize its Elapsed event. This did not work either. I tried both mentioned approaches in a Windows Forms application and they both worked.

经过一番挖掘,我发现这个网站:<一href="http://weblogs.asp.net/sibrahim/archive/2004/01/13/58429.aspx">http://weblogs.asp.net/sibrahim/archive/2004/01/13/58429.aspx在该博主建议利用另一个定时器: System.Threading.Timer 。我改变了方法,第三次和的 BOOM 的它开始像一个魅力的工作。

After some digging, I found this site: http://weblogs.asp.net/sibrahim/archive/2004/01/13/58429.aspx on which the blogger advices to utilize yet another timer: System.Threading.Timer. I changed the approach for the third time and BOOM it started working like a charm.

我的问题是:为什么我不能用其他的计时器在Windows服务,为什么会这样很难找到关于它的信息

My question is: why can't I use the other timers in Windows services and why is it so difficult to find an information about it?

推荐答案

System.Windows.Forms.Timer 定时器使用的UI消息泵编组蜱活动,服务不运行在默认情况下一个消息泵所以没有一点点额外的工作 System.Windows.Forms.Timer 定时器将无法正常工作。

The System.Windows.Forms.Timer timer uses the message pump of the UI to marshal the tick event, a service doesn't run a message pump by default so without a little extra work the System.Windows.Forms.Timer timers will not work.

System.Timers.Timer的是一个基于服务器的定时器,并提出您对创建它(我认为)的线程上的事件。如果不工作,也许你不启动定时器或线程定时器运行的立即结束(如在,无所不保活,因此完成的线程)。

The System.Timers.Timer is a server-based timer and raises an event on the thread you create it on (I think). If this isn't working, perhaps you aren't starting the timer or the timer runs on a thread that immediately ends (as in, nothing is keeping the thread alive so it finishes).

<一个href="http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx">http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

System.Threading.Timer 定时器使用运行于线程池线程,而不是绑回调该消息泵可言,所以这个工作。

The System.Threading.Timer timer uses a callback that runs on a ThreadPool thread and isn't tied to the message pump at all, hence this worked.

当您在WinForms项目执行 Application.Run(myForm的),这一呼吁也运行了消息泵,该管理界面消息。窗户定时器,你提的是一个UI组件,并预计该消息泵运行,以使蜱事件发生在UI线程上。

When you run Application.Run(myForm) in a WinForms project, that call also runs up the message pump, this manages UI messages. The windows timer you mention is a UI component and expects the message pump to be running in order to cause the tick event to occur on the UI thread.

看看这里运行在Windows服务消息泵:

Take a look here for running a message pump in a Windows Service:

在.NET Windows服务消息泵

延伸阅读:

http://support.microsoft.com/kb/842793

在最后,我只是去与 System.Threading.Timer 类。

In conclusion, I'd just go with the System.Threading.Timer class.

这篇关于为什么无法在Windows服务正常使用System.Timers.Timer的或System.Windows.Forms.Timer工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆