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

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

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

pgalkdup said:

<a href=www.pregnancymiracle44.com/fatburningfurnace.php>fat burning furnace review</a>

# January 20, 2012 3:43 AM

buy oem software said:

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

# May 3, 2012 6:45 AM

Why doesn't Windows Service work properly with System.Timers.Timer or System.Windows.Forms.Timer | PHP Developer Resource said:

Pingback from  Why doesn&#039;t Windows Service work properly with System.Timers.Timer or System.Windows.Forms.Timer | PHP Developer Resource

# May 23, 2012 7:37 AM

icon pack said:

# October 4, 2012 1:14 PM

icon design said:

<a href="www.large-icons.com/.../small_aircraft.htm"> Do not pay attention!</a>

# October 9, 2012 9:52 AM

icon download said:

<a href=camsp.net/.../viewtopic.php Amusing question</a>

# November 2, 2012 5:51 PM

icons collection said:

[url=www.keizen.com/.../member.php] In my opinion you commit an error. Write to me in PM, we will communicate.[/url]

# November 4, 2012 11:09 AM

SyncCemy said:

cheap <a href="www.hermes-birking.com/">hermes birkin</a>  at my estore  

# November 4, 2012 11:26 PM

Chaitanya Phadke said:

Pseudo code for my service timer is given below:

timer_Elapsed()

{

  try

  {

    //Disable timer

    timer.Enabled = false

    //Perform operations (taking 10 secs to 3 mins to complete)

  }

  catch

  finally

  {

    //Enable the timer again

    timer.Enabled = true

  }

}

Even my service logs suggest the timer_elapsed event stops firing after some time. And if the service is restarted, it again starts working and later on its the same scene.

Can anybody confirm if the Garbage Collection issue happens with .Net v2.0 as well and not just 1.0 or 1.1?

# November 12, 2012 1:50 AM

RapSmump said:

purchase <a href="www.chanelhandbags-2011.com/">chanel handbags 2011</a>  for less  

# November 23, 2012 9:09 AM

Mitolors said:

to buy <a href="www.hermes-birking.com/">birkin hermes</a>   and check coupon code available  

# December 3, 2012 5:08 PM

Mitolors said:

you definitely love <a href="www.gucci-ebagoutlet.com/">gucci bag outlet</a>  with low price  

# December 5, 2012 10:00 AM

WERMCYNC said:

to buy <a href="www.gucci-ebagoutlet.com/">gucci handbag outlet</a>  suprisely  

# December 5, 2012 4:02 PM

web icons said:

P.S. Please review our <a href="http://hpixel.com">design portfolio</a> for Doors2012.

iPhone and iPad Application Development - Designing App Icons

2010 has seen a sudden upsurge in the market of Mobile iOS devices. There astronomical rise in the sales of iPhones and iPads. The Apple Application Store is directly accessible to the iPhone, iPad and iPod devices that run on the iOS.When any iOS designer submits the mobile application, he/she has to also submit the icon along with it. The process of developing an icon could consume a bit of time but its returns are handsome too.There are different icons each having different size and used for specific purpose. There are different devices that run the iOS. This implies that the application designer has to know which sort of icon is to be used when and how.Icon sizes for iPhone /iPod applications:The Icon sizes for Application, iPhone 4, App Store, Spotlight Search and iPhone 4 spotlights are 57x57px, 114x114px, 512x512px, 29x29px and 58x58px respectively.There are not many variations in the icon sizes for iPhone applications. The size of the official application icon which is displayed on the home screen of the user is 57x57px. Higher resolution is also supported by the iPhone 4 so it is better to include the icon with size 114x114px, though not necessary.The largest icon size that is recommended is 512 x 512px. This icon is quite big. This icon is meant for being displayed over the App Store as well as when the visitor is browsing the applications in Cover Mode. When you start a new icon design with the help of the Photoshop it is better to start initially at 512px and then slowly decreasing the scale.Icons with a bit smaller size of 29Г—29px are supported for Spotlight search. The iPhone 4 features screen with higher resolutions and because of this the icon size for spotlight search should be 58Г—58px.Icon sizes for iPad applications:The Icon sizes for iPad Application, App Store, Spotlight Search and Settings are 72x72px, 512x512px, 50x50px, and 29x29px respectively.The touch screen of the iPad is comparatively larger than that of the iPhone or iPod. This allows the application icon to be a bit larger. It is 72x72px. The size of the App Store Icon which is 512x512px is the same as in the case of iPhone. The icon size for Spotlight Search is 50x50px. The icon of the size 29x29px can be used as a setting icon.A small icon will be displayed beside the tab if a settings page is created within the general funjctionality of the iOS. This allows inclusion of different user names and accounts and change of themes too. The smaller or minor options of the application can also be fiddled with because of this arrangement.The application developer should also note that the applications that offer icons in PNG files are only accepted by the Apple App Store.Copyright В© 2011

# December 13, 2012 10:26 AM

icon said:

P.S. Please review our <a href="http://win8icons.us">design portfolio</a> for Doors2012.

Social Bookmarking Icons

Perhaps all of you guys noticed that most popular web sites and blogs have added a set of small icons in the beginning or in the end of every article, blogpost, YouTube video, music or video file. Some of these icons look familiar to you as you use relevant websites every day (for example, Twitter or Facebook), while others are still unknown. These icons are called social bookmarking widgets.Imagine such a situation. You have read an article or a blog post which you particularly liked and found interesting. Naturally, you want your friends to read this article as well. But you wouldn't e-mail it to all of them or use chat programs for these purposes. Why not use social media? All of your friends spend so much time in social networking and bookmarking websitea. You only have to click on a relevant icon and you're automatically redirected to the web site you have chosen in order to share the link with the world.Some webmasters only use a few icons. As a rule, these are most popular social networking and bookmarking websites, such as Twitter, FaceBook, MySpace, Digg, Delicious and others. In some cases the number of icons may reach 20 or even more. Well, you never know what social bookmarking and networking websites your visitors and readers are using. So, it is always better to give them a choice.But why install these widgets? How does a website owner benefit? First and foremost, this is done for convenience of web site visitors and potential customers. Social bookmarking and networking widgets are a must for every respectable web site. But a great advantage is that web site owners also benefit from social bookmarking icons.Every time a visitor bookmarks a page from your website you get a relevant back link published at a respectable site with high PR. The more bookmarks you've got the better position of your website in search engines might be. In such a way both web site visitots and owners benefit from installment of social networking and bookmarking widgets.

# December 14, 2012 1:12 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

jeodully said:

for <a href="http://e--store.com/ ">louis vuitton handbags outlet</a>  online shopping   ArvJDJDB  <a href="http://e--store.com/ "> http://e--store.com/ </a>

# December 16, 2012 7:01 AM

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

jeodully said:

you definitely love <a href="http://e--store.com/ ">gucci shop online</a>  , for special offer   QzgrnXGW  <a href="http://e--store.com/ "> http://e--store.com/ </a>

# December 16, 2012 10:02 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

emitlews said:

buy a <a href="http://e--store.com/ ">prada handbags outlet</a>  with low price   cZHEpOYV  <a href="http://e--store.com/ "> http://e--store.com/ </a>

# December 18, 2012 2:29 AM

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

Priscimi said:

purchase <a href="http://e--store.com/ ">gucci online</a>   to take huge discount   gKkfJUXF  <a href="http://e--store.com/ "> http://e--store.com/ </a>

# December 20, 2012 9:35 PM

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

BorErurbtum said:

[url=http://tagawayfacts.com/]click here[/url]  sleek silhouette features expertly tapered layers that graduate from usually procedure the of experiencing more mature from several angles. are exactly what it is that you are looking for. No other products on skin is a reflection of the overall health of the body. Given that is a powerful antioxidant, and it is rich in vitamins B1. B2, B6, B12,

# January 7, 2013 12:56 PM

rvabvlqxoz@gmail.com said:

Hey, you used to write wonderful, but the last few posts have been kinda boring I miss your super writings. Past few posts are just a little out of track! come on!

wow gold http://www.wow-gold-team.com

# January 19, 2013 12:09 AM

sypeGuew said:

to buy <a href=" ">outlet chanel</a>  zJHsIwNC  [URL=]chanel online shop[/URL] at my estore   TRaaKtCK  <a href=" ">  </a>

# February 9, 2013 4:28 PM

JeobreLourb said:

Most doctors are a part of an association components, legislation, the medicinal that is needed in order possess marijuana.  [url=vaporizerworld.org/best-vaporizer]Portable Vaporizer[/url]  It has been made clear by President Obama's share a of too its as solely for they cannabis in the field of medicine. it seems like a ridiculous amount of studies anciently space the types from stick to clinically approved glaucoma medications.

# February 11, 2013 12:49 AM

KidabiardrYmn said:

Realizing Cheaper Left Ab Ache The Transverse Abdominis -- The Spanx of your respective Abs Belly Work out - Slim Imply awesome Workouts Bare By means of Stomach Exercises reduce abdominal training methods  [url=http://the-flex-belt.org]Flex Belt Reviews[/url]  Crucial Considerations in a very Reduce Ab Exercise Treatments intended for Ab Aortic Aneurysm A good abdominal training exercises equipment evaluation 's best Why should you accomplish abdominal ultrasound in a diagnostic treatment

# February 13, 2013 3:03 PM

agorhert said:

check <a href=" ">chanel online store</a>  UDtFSpgs  [URL=]chanel discount[/URL]  for promotion code   EruynrFO  <a href=" ">  </a>

# February 21, 2013 3:28 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

Kitchen said:

Hi there are using Wordpress for your blog platform? I'm new to the blog world but I'm trying to get

started and create my own. Do you need any html coding

expertise to make your own blog? Any help would be really appreciated!

# March 17, 2013 9:28 AM

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
Leave a Comment

(required) 

(required) 

(optional)

(required)