Samer Ibrahim's Blog

The Samer I Warrior on battles with .NET

Sponsors

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

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

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

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

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

Vijay Moharle said:

System.Windows.Forms.Timer does not work in Windows Service. It seems  the Timer_Tick event is handled differently. I used Threading timer instread of  Forms Timer and call the Timeercallback rather than an event.

# November 18, 2009 1:30 PM

James said:

Ouch! the links in the article are all dead (404 not found).

# October 12, 2010 1:44 PM

Mo Ibrahim said:

the links you provide are no longer available!

# November 5, 2010 1:30 PM

Laca said:

Server Error

404 - File or directory not found.

The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.

# January 30, 2011 11:06 AM

MSR said:

windows timer is a worst case................

it skips the display seconds(i.e, 45 to 47 or 54 to 56

# March 5, 2011 6:29 AM

Welding Electrodes said:

Hah, seriously? That's rediculous. No way

# December 18, 2011 3:37 PM

buy oem software said:

CCCfQ6 Major thanks for the blog article.Thanks Again. Want more.

# May 3, 2012 6:45 AM

binsseve said:

for <a href="http://e--store.com/ ">coach online</a>   and get big save   uOeRFcMt  <a href="http://e--store.com/ "> http://e--store.com/ </a>

# December 15, 2012 5:03 AM

Thundfut said:

check <a href="http://e--store.com/ ">lv online</a>   to get new coupon   tbbCGKsY  <a href="http://e--store.com/ "> http://e--store.com/ </a>

# December 15, 2012 8:33 PM

GotteGam said:

check <a href="http://e--store.com/ ">coach purses outlet</a>  with low price   rwWBmxlQ  <a href="http://e--store.com/ "> http://e--store.com/ </a>

# December 16, 2012 9:27 AM

GyncTund said:

must check <a href="http://e--store.com/ ">prada online outlet</a>  suprisely   uAwfbXNh  <a href="http://e--store.com/ "> http://e--store.com/ </a>

# December 17, 2012 4:59 PM

Thundfut said:

get cheap <a href="http://e--store.com/ ">coach online outlet</a>  for more detail   ELezQtZd  <a href="http://e--store.com/ "> http://e--store.com/ </a>

# December 17, 2012 7:33 PM

Kesquire said:

get <a href="http://e--store.com/ ">coach online outlet</a>  for gift   DBwhnLxk  <a href="http://e--store.com/ "> http://e--store.com/ </a>

# December 19, 2012 7:15 AM

Kesquire said:

buy best <a href="http://e--store.com/ ">coach bags outlet</a>  at my estore   LWjFKlmJ  <a href="http://e--store.com/ "> http://e--store.com/ </a>

# December 24, 2012 8:29 AM

erurtutt said:

best for you <a href="http://e--store.com/ ">coach outlets</a>  suprisely   HYKvpPPW  <a href="http://e--store.com/ "> http://e--store.com/ </a>

# December 25, 2012 11:10 PM

Beckham said:

Hi there, all is going perfectly here and ofcourse every one is sharing data, that's in fact fine, keep up writing.

# March 15, 2013 8:06 PM

Giroux said:

I always spent my half an hour to read this weblog's articles or reviews every day along with a mug of coffee.

# March 24, 2013 2:47 AM

Israel said:

We stumbled over here different website and thought I should check things

out. I like what I see so now i am following you. Look forward to checking out your web page repeatedly.

# March 24, 2013 6:45 AM

Whitt said:

Spot on with this write-up, I honestly believe that this

site needs far more attention. I'll probably be back again to read more, thanks for the advice!

# March 24, 2013 8:49 AM

Otoole said:

If you want to take a good deal from this paragraph then you

have to apply these techniques to your won web site.

# March 25, 2013 11:32 PM

Wertz said:

I've been browsing online more than 3 hours today, yet I never found any interesting article like yours. It's pretty

worth enough for me. Personally, if all webmasters and bloggers made good content as you did,

the web will be a lot more useful than ever before.

# March 25, 2013 11:49 PM

asad.naeem said:

Yes this is the case that System.timers.Timer does not fire in windows service often. You can try this code

System.Timers.Timer aTimer = new System.Timers.Timer(5000);

//If the timer is declared in a long-running method, use

// KeepAlive to prevent garbage collection from occurring before the method ends.

      GC.KeepAlive(aTimer);          

# March 28, 2013 1:42 AM

<a href="http://www.pcsafedoctor.com/">remove spyware</a> said:

hello 557697cbc43deb8fbe920e52b5c6e94e

# April 1, 2013 7:42 PM

Torrence said:

Today, I went to the beachfront with my kids. I found

a sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She put the shell to her ear

and screamed. There was a hermit crab inside and it pinched her ear.

She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

# April 15, 2013 9:24 PM