Gavin Stevens's Blog

the ramblings of another developer....

  Home  |   Contact  |   Syndication    |   Login
  33 Posts | 0 Stories | 52 Comments | 212 Trackbacks

News

Archives

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

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Wednesday, September 01, 2004 8:55 PM

Feedback

# re: System.Timers.Timer in Windows Service App 12/3/2004 6:15 PM Nik
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.

# re: System.Timers.Timer in Windows Service App 12/10/2004 3:57 AM Greg
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?

# re: System.Timers.Timer in Windows Service App 1/12/2005 8:18 AM Basicvn
You have to add reference to Timer COM

# re: System.Timers.Timer in Windows Service App 3/16/2005 5:11 PM Chris
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.



# re: System.Timers.Timer in Windows Service App 6/15/2005 1:13 AM Emmanuel Huna
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...


# re: System.Timers.Timer in Windows Service App 6/14/2006 12:45 PM Ken Eldridge
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?

# re: System.Timers.Timer in Windows Service App 12/13/2006 10:13 AM Brian Yule
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);
}
}

# re: System.Timers.Timer in Windows Service App 12/21/2006 1:39 PM Daní
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.

# re: System.Timers.Timer in Windows Service App 1/24/2007 6:54 PM Ken Hsu
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.

# re: System.Timers.Timer in Windows Service App 1/9/2008 6:51 AM Hemalatha
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.

# re: System.Timers.Timer in Windows Service App 1/14/2008 3:56 PM Mark Cunningham
Guys in the class definition you should add the following code

My class
gc.keepAlive(myTimerObject)
gc.initializelifetimeobject(mytimerObject)
End Class

# re: System.Timers.Timer in Windows Service App 2/14/2008 4:48 PM Sebastien
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!

# re: System.Timers.Timer in Windows Service App 4/3/2008 9:56 PM Adhir Dutta
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

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: