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

Windows Service with Timer Question

P: n/a
Hello everyone,

I have made a service that starts timers when it starts. I have
another windows form application that stops and starts the service. Do
I need to deal with any started timers when the service stops? Being
new at this service stuff, I don't know if it holds state or memory
and I need to stop all active timers or if the magical gc will come
along and help out.

Thanks, I hope that makes sense.

Have a great day,
Ryan

Jun 14 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Ryan wrote:
Hello everyone,

I have made a service that starts timers when it starts. I have
another windows form application that stops and starts the service. Do
I need to deal with any started timers when the service stops? Being
new at this service stuff, I don't know if it holds state or memory
and I need to stop all active timers or if the magical gc will come
along and help out.

Thanks, I hope that makes sense.
In your case here, if the timers were started on the parent thread, the
tread the service started up on, the the timers are going to be killed
when the service stops. However, if the timer was instantiated on a
spawned child thread and you don't kill the thread even though you have
stopped the service, then the child thread and the timer will still be
running.

The magical GC will kill the timer while the service is running if
you're not careful, GC.Keepalive will prevent that.

The proper way to write a timed execution in a .Net service is to use
threads on a timed basis, using the Tread.Sleep in a While True Loop.

Jun 14 '07 #2

P: n/a
I have NEVER heard that.... I was always told by people and MS people to use
the Threading.Timer for services and to always avoid while loops for this

"DArnold" <DA*****@DArnold.comwrote in message
news:eR*************@TK2MSFTNGP06.phx.gbl...
Ryan wrote:
>Hello everyone,

I have made a service that starts timers when it starts. I have
another windows form application that stops and starts the service. Do
I need to deal with any started timers when the service stops? Being
new at this service stuff, I don't know if it holds state or memory
and I need to stop all active timers or if the magical gc will come
along and help out.

Thanks, I hope that makes sense.

In your case here, if the timers were started on the parent thread, the
tread the service started up on, the the timers are going to be killed
when the service stops. However, if the timer was instantiated on a
spawned child thread and you don't kill the thread even though you have
stopped the service, then the child thread and the timer will still be
running.

The magical GC will kill the timer while the service is running if you're
not careful, GC.Keepalive will prevent that.

The proper way to write a timed execution in a .Net service is to use
threads on a timed basis, using the Tread.Sleep in a While True Loop.



Jun 14 '07 #3

P: n/a

"Smokey Grindle" <no****@nospam.comwrote in message
news:%2*****************@TK2MSFTNGP02.phx.gbl...
>I have NEVER heard that.... I was always told by people and MS people to
use the Threading.Timer for services and to always avoid while loops for
this
What MS people? When I first started writing .Net services on VS 2003 using
the Framework 1.1, a guy I worked for questioned me about why I was using a
While True loop. I told him it came right out of the VS Help for threading.

Then he came back with an article about the Whlie True loop was the
preferred method using Thread.Sleep in a .NET Service. I have been looking
for that link, but I cannot find it.

When I am writing a .NET Service or Console application, then I am going to
spawn a child thread using a delegate with a While True loop, as the worker
thread or threads. The only Timer I will use is to check thread state.

public class StartThread()
{

try
{
while true
{
do something
Thread.Sleep(timetowait)
}
}
Catch ThreadException as tex //if it hits the Catch, then it's coming
out of the loop
{
do something
}
}
Jun 14 '07 #4

P: n/a
"Smokey Grindle" <no****@nospam.comwrote in message
news:%2*****************@TK2MSFTNGP02.phx.gbl...
>I have NEVER heard that.... I was always told by people and MS people to
use the Threading.Timer for services and to always avoid while loops for
this

"DArnold" <DA*****@DArnold.comwrote in message
news:eR*************@TK2MSFTNGP06.phx.gbl...
>Ryan wrote:
>>Hello everyone,

I have made a service that starts timers when it starts. I have
another windows form application that stops and starts the service. Do
I need to deal with any started timers when the service stops? Being
new at this service stuff, I don't know if it holds state or memory
and I need to stop all active timers or if the magical gc will come
along and help out.

Thanks, I hope that makes sense.

In your case here, if the timers were started on the parent thread, the
tread the service started up on, the the timers are going to be killed
when the service stops. However, if the timer was instantiated on a
spawned child thread and you don't kill the thread even though you have
stopped the service, then the child thread and the timer will still be
running.

The magical GC will kill the timer while the service is running if you're
not careful, GC.Keepalive will prevent that.

The proper way to write a timed execution in a .Net service is to use
threads on a timed basis, using the Tread.Sleep in a While True Loop.


have a look at
http://www.codeproject.com/useritems...sp?msg=2070389
Jun 14 '07 #5

P: n/a
I concur with smokey about this

i also remember that the way to go in services is to use a threading timer

i have a few services out there running with a threading timer without anny
problems

michel posseth

"Smokey Grindle" <no****@nospam.comschreef in bericht
news:%2*****************@TK2MSFTNGP02.phx.gbl...
>I have NEVER heard that.... I was always told by people and MS people to
use the Threading.Timer for services and to always avoid while loops for
this

"DArnold" <DA*****@DArnold.comwrote in message
news:eR*************@TK2MSFTNGP06.phx.gbl...
>Ryan wrote:
>>Hello everyone,

I have made a service that starts timers when it starts. I have
another windows form application that stops and starts the service. Do
I need to deal with any started timers when the service stops? Being
new at this service stuff, I don't know if it holds state or memory
and I need to stop all active timers or if the magical gc will come
along and help out.

Thanks, I hope that makes sense.

In your case here, if the timers were started on the parent thread, the
tread the service started up on, the the timers are going to be killed
when the service stops. However, if the timer was instantiated on a
spawned child thread and you don't kill the thread even though you have
stopped the service, then the child thread and the timer will still be
running.

The magical GC will kill the timer while the service is running if you're
not careful, GC.Keepalive will prevent that.

The proper way to write a timed execution in a .Net service is to use
threads on a timed basis, using the Tread.Sleep in a While True Loop.




Jun 17 '07 #6

P: n/a

"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:OT**************@TK2MSFTNGP04.phx.gbl...
>I concur with smokey about this

i also remember that the way to go in services is to use a threading timer

i have a few services out there running with a threading timer without
anny problems
What? You think I am making this up?

I concur with what the VS 2003 Help talked about in its examples of using a
While True loop with Thread.Sleep. I also concur with the article that was
placed before me about what should be done in a timed execution of a thread
in a .NET Service application, which was to use a While True loop and
Thread.sleep. But I can't find that article, which was presented to me back
in 2004. I recall it was a MSDN article.

I have written a few .Net service and Console applications myself, and the
method I use for Treading using a sleep/wait period is what I talked about,
without any problems. The only Timer I use is to check Thread or Threads
state on a periodic basis.

..Net MCP 2003 vantage as well here and maybe MCPD 2005 vantage if I have the
energy to do it, as I am getting too old and may not go after it. But I will
go through the MS Press training kit books anyway.

Jun 17 '07 #7

P: n/a
One other thing here, MS MCTS 70-536 book has an entire chapter on
Threading. Throughout the entire chapter 7 with its examples of execution of
a thread on a timed basis, it's using a For or While loop with Tread.Sleep.
There is only one page that talks about using a System.Thread.Timer, which I
cannot say not to use a Timer. But the preferred way it seems, at least in
the examples of Threading in the book, are using a loop with Thread.Sleep.

Jun 17 '07 #8

P: n/a
every MS person I ever talked to said use threading.timers... I am talking
about in person here at tech demos and such... they always shyed away from
using while loops and sleeping

"Mr. Arnold" <MR. Ar****@Arnold.comwrote in message
news:Ol**************@TK2MSFTNGP03.phx.gbl...
One other thing here, MS MCTS 70-536 book has an entire chapter on
Threading. Throughout the entire chapter 7 with its examples of execution
of a thread on a timed basis, it's using a For or While loop with
Tread.Sleep. There is only one page that talks about using a
System.Thread.Timer, which I cannot say not to use a Timer. But the
preferred way it seems, at least in the examples of Threading in the book,
are using a loop with Thread.Sleep.

Jun 18 '07 #9

P: n/a

"Smokey Grindle" <no****@nospam.comwrote in message
news:OT**************@TK2MSFTNGP02.phx.gbl...
every MS person I ever talked to said use threading.timers... I am talking
about in person here at tech demos and such... they always shyed away from
using while loops and sleeping
That's them and I don't agree with them is the bottom line.

Jun 18 '07 #10

P: n/a
Smokey Grindle wrote:
every MS person I ever talked to said use threading.timers... I am talking
about in person here at tech demos and such... they always shyed away from
using while loops and sleeping
IMHO, there's one very Good Reason for /not/ using a Timer.

It's called JIT compilation.

You write a Service and put a Timer in it.
You code up the routine that the Timer calls.
This routine references an external Assembly.

You deploy the Service.
Somehow, you miss the dependent assembly.

Your deployed service runs!
It starts and stops perfectly!
It doesn't report /any/ errors or Exceptions!

But it never does any useful work.

Why???

When the Timer fires, the runtime attempts to JIT the method invoked by
the Timer.
With the referenced Assembly /missing/, this JIT-linking fails but the
runtime doesn't report this and, if the Exception gets logged anywhere,
I've /never/ managed to find it. You can't catch this Exception - it's
gets thrown into the depths of the runtime and the Service
Infrastructure - you never see it.

I've found that calling the "worker" method from another one inside the
service - one /with/ a loop and Sleep(s) - works 100% reliably, and you
even get to catch the Exception if you really want to. ;-)

Regards,
Phill W.
Jun 19 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.