Samer Ibrahim's Blog

The Samer I Warrior on battles with .NET

Lists/Forums/Etc.

Which Blogs do I read?

Problem with System.Timers.Timer not firing in a Windows service? Well switch to the System.Threading.Timer instead!!!

Ok so since posting about a System.Timers.Timer problem that I had quite a while ago on several newsgroups/mailing lists,  I've received several emails asking about what the solution to the problem is/was so I decided it might be about time to let everyone in on it.  Below is the basic email I've been sending out in reply to those messages...

I highly recommend that you switch to the System.Threading.Timer instead. Please read this thread, it has a couple of parts [0][1][2], it will give you great insight as to why the problem is occurring. Best of luck and I hope that helps. Regards, Sam

[1] http://discuss.develop.com/archives/wa.exe?A2=ind0307A&L=DOTNET-CLR&D=0&I=-3&P=1118

[1] http://discuss.develop.com/archives/wa.exe?A2=ind0309D&L=DOTNET-CLR&P=R5347&I=-3

[2] http://discuss.develop.com/archives/wa.exe?A2=ind0306D&L=DOTNET-CLR&P=R1007&D=0&I=-3

Comments

Joe said:

Thank god i found this I was starting to go insane. My timer was firing the first time then it stopped firing I thought I was going mad...
# February 17, 2004 12:20 PM

Ricci said:

Thankyou.. this has been driving me mad :-)
# April 30, 2004 7:13 AM

Sriram said:

Thanks so much.. :)
I was facing exactly same problem. My service used to stop after running for a long time and never used to log it in the Event Log.
# May 10, 2004 11:11 AM

Sriram said:

"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."

I think that is the problem with System.Timers.Timer not firing.

For more info refer to following article:
http://www.ftponline.com/vsm/2002_11/online/hottips/falossi/default_pf.aspx

HTH
# May 10, 2004 12:45 PM

ofsouto said:

I created a Windows Service Application and I can't start it.
When I remove the System.Timers.Timer the service starts but I need the System.Timers.Timer on application.

When I try to start the service a message shows up:
The service 'XXXX' in Local System was started and stopped. Some services are stopped automatically when there isn't no work, as Logs Service and Performance Alerts.

What's wrong? What can I do to solve this problem?

Thank's in advance.

Obede
# June 3, 2004 8:34 AM

Andy said:

The links above are dead!!!!
# June 29, 2004 10:20 AM

Naishal said:

in VC++ what can be the constructor arguments as for example TimerCallback expects two args rather than just name of proc
# July 17, 2004 2:26 AM

TrackBack said:

# September 1, 2004 3:55 PM

Leo said:

Thanks

# July 6, 2007 3:03 AM

yashpal joshi said:

hello ,

I have same problem.

any one can help me ???

# August 1, 2007 8:47 AM

grant thomson said:

I am having the same problem sam, can you please email me on grant.thomson@savante.co.uk

I have been writing a windows application using forms; have tried both system.timers.timer and forms.timer and both seem to fail.  

Do you think going to threading.timer is the silver bullet here?

# August 14, 2007 11:13 AM

NEELI said:

i have created a project and  web service to it.... the service which is i created it use the exe file of the project. but service is not running..

# October 10, 2007 2:43 AM

Peterk said:

Could some please explain how to switch from a system.windows.form.timer  to a System.Threading.Timer

# December 16, 2007 7:44 PM

PeterK said:

Sorry for posting again. I have been trawling the internet trying to find a meaningful example of System.Threading.Timer in plain english.

I am beginning to think that it doesn't.

I am trying to do something easy (so I thought). I want a service to write to a text file every  minute in VB.net 2005, it doesn't appear possible.

If you could provide something simple to do this, I would be very grateful.

# December 16, 2007 8:20 PM

Gert-Jan van der Kamp said:

Nice one! Thanks a lot, this helped me out BIGTIME!

# January 7, 2008 6:22 AM

Ajay Meher said:

The funny thing is System.Timers.Timer uses System.Threading.Timer internally ( can be found through Reflector). So its a wrapper around Threading.Timer  and microsoft tried to do some fancy things around the wrapper having some bugs in it. So its advised to use Threading.Timer.

# June 11, 2008 3:57 AM

Mark Daniel said:

There seems to be a similar problem with System.Windows.Forms.Timer not firing. I found if I called Stop() and Start() to frequently it would stop firing at all. Using System.Threading.Timer fixed the problem even though it is a forms based application

# July 16, 2008 12:42 PM

DDJames said:

Hello all

I am using system.timer bt it is not working properly

So i have decided to switch to system.threading.timers

Please someone tell me how to switch from system.timer to system.threading.timer

# August 14, 2008 3:10 AM

Bogdan Varlamov said:

By the way, I just wanted to point out that I was able to reproduce the dead timer issue on a VM server 2003 w/ SP1 which had .NET 2.0 Framework--it appears this issue is not isolated to JUST the 1.1 Framework

# September 19, 2008 3:46 PM

Crystal said:

DDJames - To switch from system.timer to system.threading.timer; go into the Designer file and manually modify all instances of the times. So if the file that has the time is Service.vb, go to the Service.Designer.vb file and modify it. Hope that helps.

# September 23, 2008 2:01 PM

Milesh said:

I am trying the examples posted on vbcity.com/.../topic.asp

private System.Timers.Timer MyTimer;

        protected override void OnStart(string[] args)

       {

           // Interval in milliseconds

           double interval = Properties.Settings.Default.TimeDelay * 60000;

           // Create timer object

           myTimer = new System.Timers.Timer(interval);

           // Set to run more than once

           timer.AutoReset = true;

           // Delegate to handle the timer event

           timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

           // Start the timer

           timer.Start();

           // Prevents garbage collection from occuring in long running methods

           GC.KeepAlive(timer);

       }

       private void timer_Elapsed(object sender, ElapsedEventArgs e)

       {

             try

             {

                     myTimer.Stop();

                      // Place call here to execute code

              }

              finally

             {

                     myTimer.Start();

              }

        }

He states that the timer stops running due to garbage collection and that every time the timer is stopped it is subjet to garbage collection. I don't know if this is the final solution but this is where I am with the process

# December 3, 2008 4:38 PM

milesh said:

This explains the issue

support.microsoft.com/.../842793

# December 3, 2008 4:44 PM

TATWORTH said:

The URLs at the start of the post all appear to be dead. Please update them if possible.

# February 25, 2009 10:27 AM

James said:

Here's a great article that will help you understand Timers in Windows:

msdn.microsoft.com/.../cc164015.aspx

# March 20, 2009 11:04 AM

Arun said:

Does the same effect for Visual Studio 2008 as well?

# July 26, 2009 9:42 AM

Tom said:

I am able to reproduce this in VS 2008 as well.

# October 13, 2009 5:51 PM

Patrik said:

Can someone help me with system.threading.. The above links are broken.. Any sample code?

patrik.lund@hssmedia.fi

# November 4, 2009 3:52 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)