System.Threading.Timer一段时间后不触发System、Threading、Timer

2023-09-04 07:21:41 作者:日暮途穷

我有一个Windows服务应用程序。而在控制台模式下运行调试它。

I have a windows service application. And debugging it by running in console mode.

下面 http://support.microsoft.com/kb/842793 这是写的Timers.Timer有一个错误,而不是射击窗口服务。而解决方法是使用Threading.Timer 而这篇文章就是对.NET 1.0和1.1

Here http://support.microsoft.com/kb/842793 it is written that Timers.Timer has a bug and not firing in windows services. And workaround is to use Threading.Timer And this article is for .NET 1.0 and 1.1

我使用.NET 4,但过一段时间后Threading.Timer也不会触发。那么有什么方法可以这样做的原因是什么? 而且你可以提出一个解决办法?

I am using .NET 4 but after some time Threading.Timer also doesn't fire. So what can be the reason for this? And what can you suggest as a workaround?

谢谢

最好的问候

编辑:我改变定时器的Threading.Timer到Timers.Timer并且工作没有任何问题。

I changed timer from Threading.Timer to Timers.Timer and it is working without any problem.

推荐答案

您保持引用您的计时器某处prevent它被垃圾收集?

Are you keeping a reference to your timer somewhere to prevent it being garbage collected?

从的文档:

只要你使用的是定时器,你   必须保持对它的引用。与   任何托管对象,一个计时器受   当有垃圾回收   没有任何引用。一个事实,即   计时器仍处于活动状态并不prevent   它被收集

As long as you are using a Timer, you must keep a reference to it. As with any managed object, a Timer is subject to garbage collection when there are no references to it. The fact that a Timer is still active does not prevent it from being collected.