problem of using timer in windows service

R

ray

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);

}
}
 
J

José Joye

You declared the "tcallback " and "tmrThreadingTimer " variable at OnStart
scope. As soon as the OnStart terminate, they get out of scope and they will
be GC.



José
 
J

José Joye

As a side note, you could have a look at this discussion thread for another
method of doing what you want:

http://www.google.ch/groups?hl=en&l..._uauthors=joye&as_scoring=d&lr=&num=100&hl=en

José
José Joye said:
You declared the "tcallback " and "tmrThreadingTimer " variable at OnStart
scope. As soon as the OnStart terminate, they get out of scope and they will
be GC.



José
ray said:
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);

}
}
 
R

ray

Thanks Jose. It works.

José Joye said:
As a side note, you could have a look at this discussion thread for another
method of doing what you want:

http://www.google.ch/groups?hl=en&l..._uauthors=joye&as_scoring=d&lr=&num=100&hl=en

José
José Joye said:
You declared the "tcallback " and "tmrThreadingTimer " variable at OnStart
scope. As soon as the OnStart terminate, they get out of scope and they will
be GC.



José
bug
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);

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top