Windows Service and Threading

T

Techno_Dex

What is the proper way to terminate a Thread in a Windows Service? I have a
windows service which starts up three threads which run in a while loop as
long as a boolean flag is set to true. These threads each sleep for X
ammount of time then start processing again. Since this is a service it's
basically running in an infinite loop. When the user goes to stop the
service, I have been calling the Thread.Abort() method, which throws a
ThreadAbortedException. I've also tried using the Thread.Interupt() method
to wake any sleeping threads, but that too throws a
ThreadInteruptedException. What is the proper way to kill these threads in
the 30 second timeframe that the windows service framework gives a service
to respond back? If catching these Exceptions are the correct way to stop
the service, what is the correct way to know if the service actually dies on
it's own for some reaon (i.e. god forbid locking)?
 
K

Kevin Spencer

Why not use a timer and run the threads when you need to, rather than
putting them to sleep? That way, all you have to do is stop the timer.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 
T

Techno_Dex

Why waste the overhead time of initializing a Thread and tearing it down
after every execution? More time and resources would be wasted getting
everything configured and up and running for the Thread each time. So back
to my original question?
 
W

William Stacey [MVP]

Running is pooling loop is not right either. A ~better way is to have
thread block on an empty work queue. A clean way to stop is to enqueue
multiple "Close" objects to the queue, so each thread will see a
Close/Shutdown object and exit nicely. Another way is to flip your flag (in
a sync context - lock) then Interrupt each thread. Each thread will get the
Interrupt exception which they will catch and drop out of their loops
respectively. Don't use Abort.

--
William Stacey [MVP]

| Why waste the overhead time of initializing a Thread and tearing it down
| after every execution? More time and resources would be wasted getting
| everything configured and up and running for the Thread each time. So
back
| to my original question?
|
| | > Why not use a timer and run the threads when you need to, rather than
| > putting them to sleep? That way, all you have to do is stop the timer.
| >
| > --
| > HTH,
| >
| > Kevin Spencer
| > Microsoft MVP
| > Professional Chicken Salad Alchemist
| >
| > What You Seek Is What You Get.
| >
| >
| > | >> What is the proper way to terminate a Thread in a Windows Service? I
| >> have a windows service which starts up three threads which run in a
while
| >> loop as long as a boolean flag is set to true. These threads each
sleep
| >> for X ammount of time then start processing again. Since this is a
| >> service it's basically running in an infinite loop. When the user goes
| >> to stop the service, I have been calling the Thread.Abort() method,
which
| >> throws a ThreadAbortedException. I've also tried using the
| >> Thread.Interupt() method to wake any sleeping threads, but that too
| >> throws a ThreadInteruptedException. What is the proper way to kill
these
| >> threads in the 30 second timeframe that the windows service framework
| >> gives a service to respond back? If catching these Exceptions are the
| >> correct way to stop the service, what is the correct way to know if the
| >> service actually dies on it's own for some reaon (i.e. god forbid
| >> locking)?
| >>
| >
| >
|
|
 
K

Kevin Spencer

You have a point. Actually, thinking back on my own solution, what I did was
to put the timer into the thread. The thread creates an instance of a class
with a timer in it, which fires and triggers an action. The class in the
thread can then be signalled to stop the timer.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 

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