473,763 Members | 8,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows service processing pattern

Originally posted to microsoft.publi c.dotnet.distri buted_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 2768
"Russ McDaniel" <rm******@spamg uard.gmail.com> wrote:
Originally posted to microsoft.publi c.dotnet.distri buted_apps with no
response. Reposted here with additional thoughts.
---

Hello,

I'm writing a Windows service which performs some potentially long-running,
uninterruptabl e 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
5559
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 context the program is running in? Basically, if it runs as a service, I would want it to call ServiceBase.Run (which will then call the execution logic). If it is run at the command-line, I simply want it to run as a normal application.
9
7272
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() methods executed but I dont see the service stopping. Please advise how to stop the service. Thanks, SP
5
1111
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 running still shutdown. Like putting it in the startup folder?? Or is that old Windows 98 terminology? For an XP machine is Windows Service for this purpose? I
2
361
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 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
5
10592
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 is a good approach to coding this type of thing? Basically, I want something like: While True -- do some processing -- sleep 10 minutes
2
5494
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 parameters except for the return values. say they pass us an id, then depending on webserice called I might need to return values, if client A calls methodA then just return name and phone but for client B calling methodA return name,phone and adress.. I want some reuasability in code (since I...
2
2259
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 the ..NET framework for this project, especially the GUI for this service and the other windows non gui .NET clients. What's the best migaration pattern for us considering not to rewrite the thousands of lines of code. The question i have are the follows.
4
6453
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 Service" that monitors the state of the first. If the "Listener Service" goes down I want the "Watcher Service" to receive a notification and perform a set action. To enable this communication I've written a separate assembly (that both services reference) that exposes two interfaces :...
0
1855
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 simple System.Timer to call the Elapsed event handler (which contains the processing code) every minute. The processing itself usually takes no more than a few seconds. Even so, I have a boolean set in the event handler when I'm currently processing information so that only one thread is...
0
9564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.