473,714 Members | 5,154 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programmaticall y stopping a Windows Service

I'm in the same boat as the fellow who posted this message back in
August:

Title : Windows Service, How does one make a service "fail" properly?
Author : Ross Bennett
Group : microsoft.publi c.dotnet.langua ges.csharp
URL : http://groups.google.com/groups?q=ro...phx.gbl&rnum=1

I need to find a way to cause the service to stop when it encounters a
fatal error during processing. Using the ServiceControll er framework
class is not an option, as doing so makes the stop appear to be
"normal" when in fact it is not.

Any ideas? Any pointers to documentation or examples?

I've been beating my head against the monitor looking for a solution
to this and have found nothing. Hard to believe that there are only
two of us out here who need to make a Windows Service behave this way.

Thanks
SFN
Nov 15 '05 #1
4 15889
Keith,

Have you tried just throwing an exception? Hopefully, the framework
will catch that and shut down the service, and indicate somehow that it was
an error that did it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Keith" <sf*****@yahoo. com> wrote in message
news:d1******** *************** ***@posting.goo gle.com...
I'm in the same boat as the fellow who posted this message back in
August:

Title : Windows Service, How does one make a service "fail" properly?
Author : Ross Bennett
Group : microsoft.publi c.dotnet.langua ges.csharp
URL : http://groups.google.com/groups?q=ro...phx.gbl&rnum=1
I need to find a way to cause the service to stop when it encounters a
fatal error during processing. Using the ServiceControll er framework
class is not an option, as doing so makes the stop appear to be
"normal" when in fact it is not.

Any ideas? Any pointers to documentation or examples?

I've been beating my head against the monitor looking for a solution
to this and have found nothing. Hard to believe that there are only
two of us out here who need to make a Windows Service behave this way.

Thanks
SFN

Nov 15 '05 #2
Can you define "fail properly"? I've had problems in the
past if unhandled exceptions occur in a windows service.
These errors cause it to hang when I attempt to "stop"
the service through the management console. However,
when I've added good error catching I haven't noticed
this problem.

Can you do the following:

1. Catch the exception
2. Write the critical error to the event log or
similarly useful location
3. Stop the service

If you're unable to stop the service from within itself,
perhaps you could spawn another process to do so...

Jerry Negrelli
Senior Software Engineer
Data Scientific Corporation
-----Original Message-----
I'm in the same boat as the fellow who posted this message back inAugust:

Title : Windows Service, How does one make a service "fail" properly?Author : Ross Bennett
Group : microsoft.publi c.dotnet.langua ges.csharp
URL : http://groups.google.com/groups? q=ross+bennett+ group:microsoft .public.dotnet. languages.csh
arp&hl=en&lr=&i e=UTF-8&oe=UTF-
8&group=microso ft.public.dotne t.languages.csh arp&selm=%
23hTHL91aDHA.23 28%40TK2MSFTNGP 09.phx.gbl&rnum =1
I need to find a way to cause the service to stop when it encounters afatal error during processing. Using the ServiceControll er frameworkclass is not an option, as doing so makes the stop appear to be"normal" when in fact it is not.

Any ideas? Any pointers to documentation or examples?

I've been beating my head against the monitor looking for a solutionto this and have found nothing. Hard to believe that there are onlytwo of us out here who need to make a Windows Service behave this way.
Thanks
SFN
.

Nov 15 '05 #3
Joy
we faced a similar issue and this is what we did.
Scenario 1 : Service is up and running and because of an application error we wanted to fail the service, so that SCM will
bring it up as per the settings in the recovery tab of the service
properties.

Environment.Exi t(-1); will indicate to the SCM that the service
failed and it will restart the service.

Scenario 2 : During startup of the service, a critical resource is
not available and have to stop the service.

=============== =============== =============== =============== =============== ==

private System.Timers.T imer timerStopCurren tService = null;
private MyService()
{
....
timerStopCurren tService = new System.Timers.T imer(1000);
timerStopCurren tService.Enable d = false;
timerStopCurren tService.Elapse d += new
System.Timers.E lapsedEventHand ler(timerStopCu rrentService_El apsed);
....
}

private void timerStopCurren tService_Elapse d(object sender,
System.Timers.E lapsedEventArgs e)
{
StopCurrentServ ice();
}

public void ShutdownService ()
{
ServiceControll er ourService = new ServiceControll er("ServiceName ");
if( ourService.Stat us == ServiceControll erStatus.Runnin g)
{
StopCurrentServ ice();
}
else
{
timerStopCurren tService.Start( );
}
}

private void StopCurrentServ ice()
{
ServiceControll er ourService = new ServiceControll er("ServiceName ");
ourService.Stop ();
}

Note : If you want to display a message box to the user use the foll.
MessageBox.Show ("Error Mesage", "Applicatio n Name",
MessageBoxButto ns.OK, MessageBoxIcon. Error,
MessageBoxDefau ltButton.Button 1,
MessageBoxOptio ns.DefaultDeskt opOnly);
=============== =============== =============== =============== =============== ==

Regards, Joy

sf*****@yahoo.c om (Keith) wrote in message news:<d1******* *************** ****@posting.go ogle.com>... I'm in the same boat as the fellow who posted this message back in
August:

Title : Windows Service, How does one make a service "fail" properly?
Author : Ross Bennett
Group : microsoft.publi c.dotnet.langua ges.csharp
URL : http://groups.google.com/groups?q=ro...phx.gbl&rnum=1

I need to find a way to cause the service to stop when it encounters a
fatal error during processing. Using the ServiceControll er framework
class is not an option, as doing so makes the stop appear to be
"normal" when in fact it is not.

Any ideas? Any pointers to documentation or examples?

I've been beating my head against the monitor looking for a solution
to this and have found nothing. Hard to believe that there are only
two of us out here who need to make a Windows Service behave this way.

Thanks
SFN

Nov 15 '05 #4
Thanks everyone for the replies.

My situation is very similar to the initial posting by Ross Bennett.

I have a service application. The actual work performed by the service
is done in a thread that is created by the main service thread. A
separate class is used to provide the ThreadStart delegate for the
thread. Starting, stopping, pausing, and continuing the service do not
present any problems. It's not an initialization problem.

The difficulty I was experiencing was in the cases where the worker
thread experiences a condition where it no longer makes sense for the
service to continue operation. An example being chronic problems with
network connections, etc.. I could not find a clean way to stop the
service so that the Windows would recognize that the service failed
and allow the recovery options in the Service Control Manager restart
the service again after a period of time.

Using the ServiceControll er class to stop the service gives the
appearance that all is well and that the service merely stopped due to
a request and not necessarily due to an error condition. Throwing an
exception from the worker thread isn't going to help, as the main
service thread will continue along happily.

I've finally settled on having the class associated with the worker
thread expose an event. The main service thread registers for the
event. The method designated by the service thread issues a call to
Environment.Exi t. When the worker thread experiences a fatal error,
the event is fired, the service thread waits for the worker thread to
die, and calls Environment.Exi t.

This is seen by Windows as a service failure and makes it eligible for
the recovery options in the Service Control Manager.

Thanks again for everyone's ideas.

jo*****@yahoo.c om (Joy) wrote in message news:<2b******* *************** ****@posting.go ogle.com>...
we faced a similar issue and this is what we did.
Scenario 1 : Service is up and running and because of an application error we wanted to fail the service, so that SCM will
bring it up as per the settings in the recovery tab of the service
properties.

Environment.Exi t(-1); will indicate to the SCM that the service
failed and it will restart the service.

Scenario 2 : During startup of the service, a critical resource is

not available and have to stop the service.

=============== =============== =============== =============== =============== ==

private System.Timers.T imer timerStopCurren tService = null;
private MyService()
{
....
timerStopCurren tService = new System.Timers.T imer(1000);
timerStopCurren tService.Enable d = false;
timerStopCurren tService.Elapse d += new
System.Timers.E lapsedEventHand ler(timerStopCu rrentService_El apsed);
....
}

private void timerStopCurren tService_Elapse d(object sender,
System.Timers.E lapsedEventArgs e)
{
StopCurrentServ ice();
}

public void ShutdownService ()
{
ServiceControll er ourService = new ServiceControll er("ServiceName ");
if( ourService.Stat us == ServiceControll erStatus.Runnin g)
{
StopCurrentServ ice();
}
else
{
timerStopCurren tService.Start( );
}
}

private void StopCurrentServ ice()
{
ServiceControll er ourService = new ServiceControll er("ServiceName ");
ourService.Stop ();
}

Note : If you want to display a message box to the user use the foll.
MessageBox.Show ("Error Mesage", "Applicatio n Name",
MessageBoxButto ns.OK, MessageBoxIcon. Error,
MessageBoxDefau ltButton.Button 1,
MessageBoxOptio ns.DefaultDeskt opOnly);
=============== =============== =============== =============== =============== ==

Regards, Joy

sf*****@yahoo.c om (Keith) wrote in message news:<d1******* *************** ****@posting.go ogle.com>...
I'm in the same boat as the fellow who posted this message back in
August:

Title : Windows Service, How does one make a service "fail" properly?
Author : Ross Bennett
Group : microsoft.publi c.dotnet.langua ges.csharp
URL : http://groups.google.com/groups?q=ro...phx.gbl&rnum=1

I need to find a way to cause the service to stop when it encounters a
fatal error during processing. Using the ServiceControll er framework
class is not an option, as doing so makes the stop appear to be
"normal" when in fact it is not.

Any ideas? Any pointers to documentation or examples?

I've been beating my head against the monitor looking for a solution
to this and have found nothing. Hard to believe that there are only
two of us out here who need to make a Windows Service behave this way.

Thanks
SFN

Nov 15 '05 #5

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

Similar topics

5
2613
by: CG | last post by:
Hi I have developed a Windows Service When I try to start the Service it tells me that it cannot start as there may not be any work to do When I comment out below line of code in my OnStart event.... objEventLog.EnableRaisingEvents = Tru ....It starts fin I have the same issue when stopping the service because of below line of code in my OnStop even
2
2886
by: Vince Keller | last post by:
I dont know if this is the correct newsgroup to post questions on Windows Service. I am trying to install and uninstall a Windows Service programmatically. As far as I know, there isnt a class in .NET FCL which supports doing that. The ServiceController class starts/stops an installed service but doesnt install a service. Is there a way to install/uninstall a service programmatically (not using the Installer) ? Thanks.
0
1546
by: Daniel O'Brien | last post by:
Hi - any help with this would be greatly appreicated - it has already had me confused for a good few hours! I am using Visual Studio 2003 and the .NET framework 1.1. I have a C# Windows Service, with my own class extending from ServiceBase in the normal fashion. The service starts a remoting object (WKO and singleton) which many clients can then make calls on. Clients may also register with events on the server such that the server...
9
7021
by: Hardy Wang | last post by:
Hi all: I read an article from http://www.c-sharpcorner.com/Code/2003/Sept/InstallingWinServiceProgrammatically.asp about how to install a windows service programmatically. Based ont the code sample, it provides the feature to install service under LocalSystem account. What I need is to install service under some other certian account. By further studying the code, and MSDN...
7
10066
by: Gene | last post by:
I have a Windows Service (VB.NET) that runs 24/7. It queries a Web service 5 to 10 times per hour. About 2 or 3 times a month, it fails. The log indicates that it sends the request to the Web service but there is no return from the service. Stopping and restarting the Windows service cures the problem. Our desire is to cure the problem with appropriate error handling but failing that, is there an easy way to automatically stop and...
0
1424
by: Glen Wolinsky | last post by:
I am creating a Windows service that will check a request queue (database) for pending requests. It will then process each individual request until completed, wait a set time interval and then move on to the next one in the list. The processing routine could take 1-2 minutes to finish processing one request. My question is this: If the service is stopped while the process routine is running, how can I make sure the processing of that...
2
1702
by: Ian | last post by:
I'm investigating a requirement where we've got a number of servers running the same web service using network load balancing to provide resilience. In the event that the web service encounters a fatal error is it possible to get the web service to programmatically stop the IIS site it's running on so that NLB will not direct requests to it until the fault is fixed and the site then started again? If not is there another way to prevent NLB...
6
8110
by: D | last post by:
I have a simple file server utility that I wish to configure as a Windows service - using the examples of the Python Win32 book, I configured a class for the service, along with the main class functions __init__, SvcStop, and SvcDoRun (which contains my server code). After registering the service, I am able to start it with no problems. However, it never stops correctly (net stop returns "service could not be stopped") and service is left...
10
7536
by: archana | last post by:
Hi all, I am having one windows service which is updating to database. On 'Onstop i want to wait till current updation complete. How will i do this? Because if i write some lengthy code on onstop it fails to stop and status remain 'stopping' Later i can't do anything on that service even i can't uninstall that service.
0
8712
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9316
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9179
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
9077
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
7954
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
6638
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
5959
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
4466
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...
2
2525
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.