Windows Service with Timer Question

R

Ryan

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
 
D

DArnold

Ryan said:
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.
 
S

Smokey Grindle

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
 
M

Mr. Arnold

Smokey Grindle said:
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
}
}
 
M

Michel Posseth [MCP]

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
 
M

Mr. Arnold

Michel Posseth said:
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.
 
M

Mr. Arnold

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.
 
S

Smokey Grindle

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
 
M

Mr. Arnold

Smokey Grindle said:
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.
 
P

Phill W.

Smokey said:
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.
 

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