I have a windows service application. And debugging it by running in console mode.
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
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?
Thanks,
Best Regards
EDIT: I changed timer from Threading.Timer to Timers.Timer and it is working without any problem.
Your timer object goes out of scope and gets erased by Garbage Collector after some time, which stops callbacks from firing.
Save reference to it in a member of class.
Here is how you can use RegisterWaitForSingleObject:
When you have code registering a timer like that:
You can have this:
You can use allTasksWaitHandle.Set() after ServiceBusTimerCallback to inform user that task was done, so it will run task again immediately. This code posted above will run task after timeont will pass, so every 20 seconds.
I implemented it in WebAPI project and it works great.
Work around?
Personally, I suggest using a RegisterWaitForSingleObject function as opposed to timers for the exact reason you are running into. The RegisterWaitForSingleObject registers a delegate to be called on interval that you set analgous to a timer and are super easy to implement. You could have a test harness up and running in a matter of hours. I use this method of interval firing in my Windows Services and it is a tried and true stable solution that works for me.
Read the link below and goto the links within the article for code samples and walkthroughs.
Running a Periodic Process in .NET using a Windows Service:
http://allen-conway-dotnet.blogspot.com/2009/12/running-periodic-process-in-net-using.html
Complete example of a windows service using enterprise library for logging and recurrent threads. Remove logger.write lines if not using entreprise library
Are you keeping a reference to your timer somewhere to prevent it being garbage collected?
From the docs: