471,339 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,339 software developers and data experts.

Handle STOP in a multi-threaded Windows service

Hi

I've written a Windows Service which has
- several (0-100) listeners threads which spawn worker threads based on
events, timers etc
- several (0-300) worker threads which handle data (message based) and
terminate after processing

I'm wondering what exactly happens when I stop the service. I don't see an
ThreadAbortedException, but I'm worried my worker threads might be
terminated in the middle of processing. My Server.OnStop is empty.

Does Windows wait for all threads to terminate in a given timespan, then
send an Abort to all running threads? Or does it send Abort directly?

Any help, including pointing to good URL (haven't found any so far) or
better newsgroup is very much appreciated.

Simon
Jul 21 '05 #1
2 7044
Inline

Willy.

"Simon Niederberger" <si***@sincore.ch> wrote in message
news:Om*************@TK2MSFTNGP14.phx.gbl...
Hi

I've written a Windows Service which has
- several (0-100) listeners threads which spawn worker threads based on
events, timers etc
- several (0-300) worker threads which handle data (message based) and
terminate after processing

Such high number of threads is just a waste of CPU and memory resources, you
can do with far less than that.

I'm wondering what exactly happens when I stop the service. I don't see an
ThreadAbortedException, but I'm worried my worker threads might be
terminated in the middle of processing. My Server.OnStop is empty.

Does Windows wait for all threads to terminate in a given timespan, then
send an Abort to all running threads? Or does it send Abort directly?
The OS doesn't send anything at all, the CLR stops the threads cold by
issuing a Thread.Abort as part of the CLR's orderly shutdown procedure. If
this is not what you want, you should handle your thread shutdown yourself
in OnStop. One way of doing this is to have your threads inspect a shared
flag that you could set in OnClose, once the flag set, the active thread
should stop it's activity and return, the OnStop procedure can use
Thread.Join(timeout) on each thread you have started.
Note that all this should be done within 30 seconds, as otherwise the SCM
will consider that the service failed to shutdown.
Any help, including pointing to good URL (haven't found any so far) or
better newsgroup is very much appreciated.

Simon

Jul 21 '05 #2
Thanks a lot, perfect answer (within hours!) I'll look into reducing the
number of threads, but I feel that won't be to easy.

Cheers
Simon
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:ui**************@TK2MSFTNGP09.phx.gbl...
Inline

Willy.

"Simon Niederberger" <si***@sincore.ch> wrote in message
news:Om*************@TK2MSFTNGP14.phx.gbl...
Hi

I've written a Windows Service which has
- several (0-100) listeners threads which spawn worker threads based on
events, timers etc
- several (0-300) worker threads which handle data (message based) and
terminate after processing

Such high number of threads is just a waste of CPU and memory resources,

you can do with far less than that.

I'm wondering what exactly happens when I stop the service. I don't see an ThreadAbortedException, but I'm worried my worker threads might be
terminated in the middle of processing. My Server.OnStop is empty.

Does Windows wait for all threads to terminate in a given timespan, then
send an Abort to all running threads? Or does it send Abort directly?

The OS doesn't send anything at all, the CLR stops the threads cold by
issuing a Thread.Abort as part of the CLR's orderly shutdown procedure. If
this is not what you want, you should handle your thread shutdown yourself
in OnStop. One way of doing this is to have your threads inspect a shared
flag that you could set in OnClose, once the flag set, the active thread
should stop it's activity and return, the OnStop procedure can use
Thread.Join(timeout) on each thread you have started.
Note that all this should be done within 30 seconds, as otherwise the SCM
will consider that the service failed to shutdown.
Any help, including pointing to good URL (haven't found any so far) or
better newsgroup is very much appreciated.

Simon


Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Leandro Berti via DotNetMonster.com | last post: by
12 posts views Thread by Aaron Smith | last post: by
4 posts views Thread by Niels Jensen | last post: by
4 posts views Thread by Jackson Peebles | last post: by
reply views Thread by =?Utf-8?B?aGVyYmVydA==?= | last post: by
4 posts views Thread by Rui Maciel | last post: by
4 posts views Thread by MartinRinehart | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.