Home All Groups Group Topic Archive Search About

Thread management in a Windows service

Author
11 Jan 2006 3:17 PM
Gregory Gadow
I have a service written in VB.Net 2.0 that watches a folder for text
files and, when a text file is placed in the folder, moves it elsewhere
and processes it. I would like to optimize the service by putting each
document on its own worker thread.

My main question is: what would happen if a service shut-down command is
issued and there are pending threads? I am hoping that the service will
wait until all of the worker threads have terminated before shutting
down, but I fear it won't be that easy. Any suggestions -- or better,
code examples available on the web -- would be much appreciated.
--
Gregory Gadow

Author
11 Jan 2006 6:16 PM
Scott Swigart
I don't have a code example handy, but probably the easiest way to do work in
the background is using the new BackgroundWorker component.  I believe that
when a service gets a shutdown notification, it will end up calling your
service code to do the shutdown.  In here, you could either wait for the
background workers to complete, or send a cancel to them to make them
terminate.

Show quoteHide quote
"Gregory Gadow" wrote:

> I have a service written in VB.Net 2.0 that watches a folder for text
> files and, when a text file is placed in the folder, moves it elsewhere
> and processes it. I would like to optimize the service by putting each
> document on its own worker thread.
>
> My main question is: what would happen if a service shut-down command is
> issued and there are pending threads? I am hoping that the service will
> wait until all of the worker threads have terminated before shutting
> down, but I fear it won't be that easy. Any suggestions -- or better,
> code examples available on the web -- would be much appreciated.
> --
> Gregory Gadow
>
>
>