By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,356 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,356 IT Pros & Developers. It's quick & easy.

problem of using timer in windows service

P: n/a
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...b;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);

}
}
Jul 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <fu********@yahoo.com> wrote in message
news:ak********************@news01.bloor.is.net.ca ble.rogers.com...
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...b;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);

}
}

Jul 21 '05 #2

P: n/a
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&lr...D100%26hl%3Den

Josť
"Josť Joye" <jo*******@KILLTHESPAMSbluewin.ch> wrote in message
news:Oq**************@TK2MSFTNGP09.phx.gbl...
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" <fu********@yahoo.com> wrote in message
news:ak********************@news01.bloor.is.net.ca ble.rogers.com...
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...b;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);

}
}


Jul 21 '05 #3

P: n/a
ray
Thanks Jose. It works.

"Josť Joye" <jo*******@KILLTHESPAMSbluewin.ch> wrote in message
news:eZ*************@TK2MSFTNGP09.phx.gbl...
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&lr...D100%26hl%3Den
Josť
"Josť Joye" <jo*******@KILLTHESPAMSbluewin.ch> wrote in message
news:Oq**************@TK2MSFTNGP09.phx.gbl...
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" <fu********@yahoo.com> wrote in message
news:ak********************@news01.bloor.is.net.ca ble.rogers.com...
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...b;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);

}
}



Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.