Geeks With Blogs
Gavin Stevens's Blog the ramblings of another developer....

I had this problem a while back and recently revisited it, so I figured I would share if anyone didn't know this:

System.Timers.Timer CAN HAVE PROBLEMS IN A WINDOWS SERVICE!!!

Sorry for yelling, but maybe that will catch some attention, yes its true, I drove myself nuts on a service I was writing once, everything seems to work fine until one time, the timer just stops firing.

"Server-based timers use the thread pool internally, and the event handler runs in a thread taken from the pool. For this reason, conflicts might occur while the event handler is accessing shared variables and modifying controls and forms."

Resources:

http://www.ftponline.com/vsm/2002_11/online/hottips/falossi/default_pf.aspx

http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/default.aspx

http://weblogs.asp.net/sibrahim/archive/2004/01/13/58429.aspx

http://www.eggheadcafe.com/ng/microsoft.public.dotnet.internationalization/post115585.asp

Choose your timers wisely, nothing like a timer that stops firing for no apparent reason to drive you nuts!

Gavin

Posted on Wednesday, September 1, 2004 8:55 PM | Back to top


Comments on this post: System.Timers.Timer in Windows Service App

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
I've experienced the same problem. Thanks for posting this, I've been going crazy trying to figure out what the problem is with my service and why the timer stops firing after a while.
Left by Nik on Dec 03, 2004 6:15 PM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
I think you mean to say in the original post by Gavin that SYSTEM.WINDOWS.FORMS.TIMERS can cause them to not work in windows services.

Correct?
Left by Greg on Dec 10, 2004 3:57 AM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
You have to add reference to Timer COM
Left by Basicvn on Jan 12, 2005 8:18 AM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
Same Problem... runs for a full day then nothing.....

Noticed though that my threads went from 14 to 45 over the course of the day.

I do not do any thread work myself.. just using the system.timers.timer.

Left by Chris on Mar 16, 2005 5:11 PM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
Unfortunately in my case it doesn't matter whether I use system.timers.timer or system.threading.timer. Everything will work fine for 24-48 hours, until all of a sudden one of the timers will stop working.

This only happens in Windows 2003 and never happens in Windows 2000 Server.

I have three timers firing at different intervals (2 seconds, 10 seconds and 12 seconds). It's not always the same timer that stops working - it's completely random.

The only solution I found is for one timer to make sure that the another timer is properly working, but that was a pain...
Left by Emmanuel Huna on Jun 15, 2005 1:13 AM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
I have the exact same problem with timers stopping in a Windows Service running on Windows 2003 Server.
Originally I was using the System.Threading.Timer, Microsoft recommends the System.Timer.Timer, both yield the same results.
Like Emmanuel Huna I also developed a technique of have Timers watch each other, but this also eventually fails over a longer period of time.
Any further suggestions for this problem?
Left by Ken Eldridge on Jun 14, 2006 12:45 PM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
I have two seperate services using System.Timers.Timer with the same issue.
Nothing in the event log to indicate why the timer would stop running. I'm going to try and use the Threading timer without using periodic invocations.

private void ServiceTimer_Elapsed(object sender)
{
try
{
Kickoff();
}
finally
{
ServiceTimer.Change(5000, System.Threading.Timeout.Infinite);
}
}
Left by Brian Yule on Dec 13, 2006 10:13 AM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
I was having the same problem using both, system.Timers.Timer and System.Threading.Timer. I solved it using the ThreadPool.RegisterWaitForSingleObject method. This method takes as parameters a WaitHandle, a WaitOrTimerCallaback, a state object, the time in miliseconds to wait and a bool that specifies if you want the callback being called once or not. If you never signal the waithandler you pass as a parameter and specfie that the callback must be called for ever, you have a timer.
Left by Daní on Dec 21, 2006 1:39 PM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
I had the same problem in Windows Service, and called Microsoft for the explaination. They told me to write a small app to demo the problem. So, I did a Windows exe with the Timer execution, and had the problem exist. This problem doesn't just occure in Windows Service, it will happen whenever you launch many instances using the Timer.
Left by Ken Hsu on Jan 24, 2007 6:54 PM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
Im having one doubt regarding timer events.Im using windows service,i want to monitor lan connectivity checking for every 10 seconds and for disconnect status for every 45 seconds.How can i monitor this function within one timer interval.

Hope,u would reply for my post.
Left by Hemalatha on Jan 09, 2008 6:51 AM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
Guys in the class definition you should add the following code

My class
gc.keepAlive(myTimerObject)
gc.initializelifetimeobject(mytimerObject)
End Class
Left by Mark Cunningham on Jan 14, 2008 3:56 PM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
Hi Guys,

I'm having the same problem with a widows service that uses a timer to check a specific folder. Every minutes the timer launches a function that checks a folder and treats any files within this folder.

It worked fine for few months and stopped working. I tried to reboot the server, reinstall the windows service, but nothing happens.

Any idea of what I could do to solve the problem?

Thanks in advance!
Left by Sebastien on Feb 14, 2008 4:48 PM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Const iTIME_INTERVAL As Integer = 60000 ' 1 min.
Dim oTimer As System.Threading.Timer
System.IO.File.AppendAllText("C:\NPulpXfer.txt", Now.ToString() + " =>NPulpXfer Service has been started at " + ControlChars.Lf)
Try
Dim tDelegate As Threading.TimerCallback = AddressOf EventAction
oTimer = New System.Threading.Timer(tDelegate, Me, 0, iTIME_INTERVAL)
oTimer.GetLifetimeService()
Catch Ex As Exception
System.IO.File.AppendAllText("C:\NPulpXfer.txt", Now.ToString() + ":-> NPulpXfer Service Error =<>" + Ex.ToString() + ControlChars.Lf)

End Try

End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub
Public Sub EventAction(ByVal sender As Object)
'System.IO.File.AppendAllText("C:\NPulpXfer.txt", "NPulpXfer Service fires EventAction at " & Now.ToString() + ControlChars.Lf)
'=============== Data Transfer Start =====================
'ServiceRefresh()
NPS_GetOracleData()
Pulp2_OLEDB()
TrackMsg() 'INFORMATION of Row Number
'ServiceRefresh()
'=============== Data Transfer End =======================
End Sub


Public Sub ExceptionMsgPrint(ByVal exMsg As System.Exception, ByVal Area As String)
System.IO.File.AppendAllText("C:\NPulpXfer.txt", Now.ToString() + "[" + Area + "]" + ":= " + exMsg.Message.ToString() + ControlChars.Lf)
End Sub
Public Sub TrackMsg()
System.IO.File.AppendAllText("C:\NPulpXfer.txt", Now.ToString() + ": Steam[" + STM_RINDEX + "] & Elec[" + ELC_RINDEX + "]th" + " row Data is Transfered " + ControlChars.Lf)
End Sub

================================================================================

I have used this code but after 28/32 times Timer is stopped without any notice...

Help please
Left by Adhir Dutta on Apr 03, 2008 9:56 PM

# re: System.Timers.Timer in Windows Service App
Requesting Gravatar...
Hi,
I had the same problem , My window service application was working fine for months, it is a multithreading application with background threads and timers (System.Timers.Timer), Suddenly some timers start not to work. But other threads in the application are working fine . After the restarting the service it startes to work fine. I don't know what is the priority of the background thread and timers. And the environment in which we have deployed this application is a
Virtual machine windows server 2003. Any one with this kind of deployment getting issue like this ?.

Thank with best regards
Praburaj Vishvanathan
Left by Praburaj Vishvanathan on Jul 12, 2012 6:54 AM

Your comment:
 (will show your gravatar)


Copyright © Gavin Stevens | Powered by: GeeksWithBlogs.net