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