Stopping a threaded .Net service

  • Thread starter Thread starter Jim Adams
  • Start date Start date
J

Jim Adams

When a VB.Net windows service receives a Stop request, when does it
actually end? For example, when a service app spawns worker threads
that run for a period ranging from a few seconds to a few minutes, how
will a Stop request be handled.

Thanks,

Jim
 
Jim Adams said:
When a VB.Net windows service receives a Stop request, when does it
actually end? For example, when a service app spawns worker threads
that run for a period ranging from a few seconds to a few minutes, how
will a Stop request be handled.

I believe it's basically up to you to stop the other threads. See
http://www.pobox.com/~skeet/csharp/threads/shutdown.shtml for my
suggestions on orderly thread shutdown.
 
Jim Adams said:
When a VB.Net windows service receives a Stop request, when does it
actually end? For example, when a service app spawns worker threads
that run for a period ranging from a few seconds to a few minutes, how
will a Stop request be handled.

That's your responsibility.

In your OnStop method you need to tear down the service. This includes
closing files, shutting down threads, etc. Threads marked as background
threads can be automatically terminated when your application is shut down,
but you should do this yourself. In OnStop you should signal then join each
thread you've spawned. After waiting for the threads to stop, you can
Interrupt them if they haven't finished, or you can let them continue.

David
 

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

Back
Top