472,352 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Windows service processing pattern

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
Jul 21 '05 #1
2 2652
"Russ McDaniel" <rm******@spamguard.gmail.com> wrote:
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.
I would agree, the SCM does get ruthless.
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.
Somehow I don't see how you could fool the SCM for very long
- its going to terminate the service process.
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


While I'm not sure how legitimate the requirement is to want
to stop a service, while allowing a long running task to
complete (what if the machine is shutting down? You are not
going to get to complete the task anyway) consider splitting
the service into a service and a worker process. When the
service starts up:

- Service checks for the existance of worker process. If
worker process does not exist, skip to last step.

- Attempt to communicate with the existing worker process.
if this fails after a number of (configurable) attempts each
spaced with by a (configurable) time interval, terminate the
worker process, then skip to last step.

- Once the worker process responds, queue up new requests
with the intention of fowarding them later to the new worker
process. In regular (configurable) intervals query the
pre-existing process for evidence of progress. If it fails
to show progress terminate the worker process and skip to
last step.

- Proceed once the existing worker process exits.

- Start new worker process. Issue any queued up requests.

In effect your service would be acting as a kinder, gentler
SCM.

You may choose to start the new process before the existing
one completes - but then you have to handle the added
complexity of managing (potentially) multiple pre-existing
processes and terminating them in case they stop showing any
progress.
Jul 21 '05 #2
Thanks for your suggestions. It may be a little too sophisticated for the
application in question, but I'll see if it makes sense to incorporate any
of it into my current design. With regard to this point:
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.


Somehow I don't see how you could fool the SCM for very long
- its going to terminate the service process.


I've actually tested this scenario. The SCM doesn't appear to care about
what the process is actually doing, it only cares about what the process
says it's doing. For example, my shutdown mechanism in the Stop event
handler is to simply flip my internal processing state from Started to
Stopping. The main processing loop of my worker thread in my service then
exits when it gets around to inspecting its process state on the next loop
iteration.

Since the Stop event handler immediately returns, the SCM assumes my service
stopped and doesn't wait or complain. Meanwhile, my service process is still
winding down in the background. Granted, if the system is shutting down,
it's going to get killed anyway, but at least it has a mechanism for
shutting down gracefully if it is manually stopped.

Thanks again,
Russ
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Rilling | last post by:
I have an EXE that I would like to be able to run from either the command-line or as a windows service. Is there a way that I can tell which...
9
by: SP | last post by:
Hi All, I wrote a windows service which is supposed to stop after specified amount of time. I am calling OnStop() after specified time. OnStop()...
5
by: BobAchgill | last post by:
I have a Form that on a timer uses FTP to update data on the local disk. I would like this Form to start on boot of the computer and stay...
2
by: Russ McDaniel | last post by:
Originally posted to microsoft.public.dotnet.distributed_apps with no response. Reposted here with additional thoughts. --- Hello, I'm...
5
by: eb65 | last post by:
I have a need to write a Windows Service application in VB.Net that needs to continuously do some processing, wait ten minutes, then repeat. What...
2
by: asha | last post by:
Hi, I am new to .net so excuse me. I need to create mutiple webservices (for multiple clients). The webmethods would essentially have the same...
2
by: shyam | last post by:
I have a C++ COM based windows service which have more than 30k lines of code and which is stablized over years. We need to take the advantage of...
4
by: cmgarcia17 | last post by:
I have two windows services that I've written: the first is a "Listener Service" that listens for MSMQ Message delivery. The second is a "Watcher...
0
by: =?Utf-8?B?QmlsbEI=?= | last post by:
This is a tough one... My Windows Service app periodically performs some processing on new entries to a SQL Server database table. It uses a...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.