为什么 Windows 服务不能与 System.Timers.Timer 或 System.Windows.Forms.Timer 一起正常工作 [英] Why doesn't Windows Service work properly with System.Timers.Timer or System.Windows.Forms.Timer

查看:16
本文介绍了为什么 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 及其 Tick 事件.我选择它,因为我正在阅读教程.不知何故,我无法使服务正常工作.它安装并启动没有问题,但它不会触发事件(我将调试器附加到进程并看到它没有被触发).我认为在 Windows 服务中使用 Forms 计时器可能不是一个好主意,所以我决定切换到 System.Timers.Timer 并利用它的 Elapsed 事件.这也不起作用.我在 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.

经过一番挖掘,我找到了这个网站: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 的消息泵来封送滴答事件,服务不会t 默认情况下运行消息泵,因此如果没有一点额外的工作,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).

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

System.Threading.Timer 计时器使用在 ThreadPool 线程上运行的回调,并且根本不绑定到消息泵,因此这是有效的.

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 消息.您提到的 Windows 计时器是一个 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天全站免登陆