System.Threading.Timer 一段时间后不触发 [英] System.Threading.Timer not firing after some time
问题描述
我有一个 Windows 服务应用程序.并通过在控制台模式下运行进行调试.
I have a windows service application. And debugging it by running in console mode.
这里 http://support.microsoft.com/kb/842793据说 Timers.Timer 有一个错误并且不会在 Windows 服务中触发.解决方法是使用 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.
推荐答案
您是否在某处保留对计时器的引用以防止它被垃圾收集?
Are you keeping a reference to your timer somewhere to prevent it being garbage collected?
来自文档:
只要您使用计时器,您就可以必须保留对它的引用.与任何托管对象,一个 Timer 是主题当有垃圾收集时没有提及它.事实是定时器仍处于活动状态不会阻止它从被收集.
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.
这篇关于System.Threading.Timer 一段时间后不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!