R
Russ McDaniel
Originally posted to microsoft.public.dotnet.distributed_apps with no
response. Reposted here with additional thoughts.
---
Hello,
I'm writing a Windows service which performs some potentially long-running,
uninterruptable processes. When the service starts, I create a separate
worker thread to perform the work so as to not block the service control
manager upon startup. The design quandry I'm running into though is how best
to stop the service while it may be in the middle of a long-running process
that I do not wish to terminate.
My options as I see it are as follows:
1. During the service stop call, set my process state to indicate to the
worker thread that it should exit at the next opportunity. Then call
Thread.Join to wait for the process to complete. This doesn't really seem
like a viable option since it will likely cause the service control manager
to timeout.
2. During the service stop call, set my process state to indicate to the
worker thread that it should exit at the next opportunity. Exit immediately
and allow the thread to run its course while the service control manager
assumes that my service has stopped. Meanwhile, the service may continue to
run for a while until the current process completes. There are potential
problems here as well including if the user would attempt to restart the
service immediately while the process is still running.
One idea to mitigate the restart problem might be to use a Mutex to prevent
multiple instances of the app from running. With some extra work, it could
signal the original process to continue running if it is waiting to stop.
Does number 2 sound reasonable? Are there other ways I can approach this?
Regards,
Russ
response. Reposted here with additional thoughts.
---
Hello,
I'm writing a Windows service which performs some potentially long-running,
uninterruptable processes. When the service starts, I create a separate
worker thread to perform the work so as to not block the service control
manager upon startup. The design quandry I'm running into though is how best
to stop the service while it may be in the middle of a long-running process
that I do not wish to terminate.
My options as I see it are as follows:
1. During the service stop call, set my process state to indicate to the
worker thread that it should exit at the next opportunity. Then call
Thread.Join to wait for the process to complete. This doesn't really seem
like a viable option since it will likely cause the service control manager
to timeout.
2. During the service stop call, set my process state to indicate to the
worker thread that it should exit at the next opportunity. Exit immediately
and allow the thread to run its course while the service control manager
assumes that my service has stopped. Meanwhile, the service may continue to
run for a while until the current process completes. There are potential
problems here as well including if the user would attempt to restart the
service immediately while the process is still running.
One idea to mitigate the restart problem might be to use a Mutex to prevent
multiple instances of the app from running. With some extra work, it could
signal the original process to continue running if it is waiting to stop.
Does number 2 sound reasonable? Are there other ways I can approach this?
Regards,
Russ