Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2004

Tip: Looking for answers? Try searching our database.

problem of using timer in windows service

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ray - 04 Aug 2004 06:18 GMT
Hi,

I just wrote a windows service application to call a function of another
object periodically. I used System.Server.Timer and I found that it works
fine within the first 2 hours but the onTimer event wouldn't be raised again
after few hours. I checked the msdn and Microsoft claims that it is a bug in
their .net framework as follows:

http://support.microsoft.com/default.aspx?scid=kb;en-us;842793

Microsoft suggests us to use System.Threading.Timer instead of
System.Server.Timer. I tried to use System.Threading.Timer but it only works
in the first minute and then no response. The service even couldn't be
stopped or paused. Can anyone help me how to use System.Threading.Timer in
windows service application? Where can I get a sample?

The following is my code:

Thank you !
Ray

using System.Threading;

public class PollingService : System.ServiceProcess.ServiceBase
{
 protected PivotalWS ws = new PivotalWS();
 protected Timer tmrThreadingTimer;

 protected override void OnStart(string[] args)
 {
  // TODO: Add code here to start your service.

  EventLog.WriteEntry("Starting Polling Service");

  // callback delegate
  TimerCallback tcallback = new TimerCallback(ws.SendTopQueueMessage) ;

  // instantiate the Timer object
  Timer tmrThreadingTimer = new Timer(tcallback, null, 0, 10 * 1000) ;

  //Manually start the timer...
  //tmrThreadingTimer.Change(0, 10 * 1000);

}

 protected override void OnStop()
 {
  // TODO: Add code here to perform any tear-down necessary to stop your
service.
  EventLog.WriteEntry("Stopping Polling Service");

  //Manually stop the timer...
  //tmrThreadingTimer.Change(Timeout.Infinite, Timeout.Infinite);

 }
}
Jakob Christensen - 04 Aug 2004 07:37 GMT
Hey Ray,

Your timer does not work because you have declared your Timer variable (tmrThreadingTimer) as a local variable inside the OnStart method.  This "overrides" the declaration of tmrThreadingTimer as a membervariable.  You need to change the following line

Timer tmrThreadingTimer = new Timer(tcallback, null, 0, 10 * 1000) ;

to

tmrThreadingTimer = new Timer(tcallback, null, 0, 10 * 1000) ;

I think this will solve your problem.

Regards, Jakob.

> Hi,
>
[quoted text clipped - 52 lines]
>   }
> }
ray - 05 Aug 2004 03:24 GMT
Thanks Jakob. It works

> Hey Ray,
>
> Your timer does not work because you have declared your Timer variable (tmrThreadingTimer) as a local variable inside the OnStart method.  This
"overrides" the declaration of tmrThreadingTimer as a membervariable.  You
need to change the following line

> Timer tmrThreadingTimer = new Timer(tcallback, null, 0, 10 * 1000) ;
>
[quoted text clipped - 62 lines]
> >   }
> > }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.