By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

Programmatically stopping a Windows Service

P: n/a
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.public.dotnet.languages.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 ServiceController 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
Share this Question
Share on Google+
4 Replies


P: n/a
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.com

"Keith" <sf*****@yahoo.com> wrote in message
news:d1**************************@posting.google.c om...
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.public.dotnet.languages.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 ServiceController 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

P: n/a
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.public.dotnet.languages.csharp
URL : http://groups.google.com/groups? q=ross+bennett+group:microsoft.public.dotnet.langu ages.csh
arp&hl=en&lr=&ie=UTF-8&oe=UTF-
8&group=microsoft.public.dotnet.languages.csharp&s elm=%
23hTHL91aDHA.2328%40TK2MSFTNGP09.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 ServiceController 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

P: n/a
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.Exit(-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.Timer timerStopCurrentService = null;
private MyService()
{
....
timerStopCurrentService = new System.Timers.Timer(1000);
timerStopCurrentService.Enabled = false;
timerStopCurrentService.Elapsed += new
System.Timers.ElapsedEventHandler(timerStopCurrent Service_Elapsed);
....
}

private void timerStopCurrentService_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
StopCurrentService();
}

public void ShutdownService()
{
ServiceController ourService = new ServiceController("ServiceName");
if( ourService.Status == ServiceControllerStatus.Running)
{
StopCurrentService();
}
else
{
timerStopCurrentService.Start();
}
}

private void StopCurrentService()
{
ServiceController ourService = new ServiceController("ServiceName");
ourService.Stop();
}

Note : If you want to display a message box to the user use the foll.
MessageBox.Show("Error Mesage", "Application Name",
MessageBoxButtons.OK, MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
================================================== ===========================

Regards, Joy

sf*****@yahoo.com (Keith) wrote in message news:<d1**************************@posting.google. 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.public.dotnet.languages.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 ServiceController 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

P: n/a
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 ServiceController 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.Exit. 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.Exit.

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.com (Joy) wrote in message news:<2b**************************@posting.google. 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.Exit(-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.Timer timerStopCurrentService = null;
private MyService()
{
....
timerStopCurrentService = new System.Timers.Timer(1000);
timerStopCurrentService.Enabled = false;
timerStopCurrentService.Elapsed += new
System.Timers.ElapsedEventHandler(timerStopCurrent Service_Elapsed);
....
}

private void timerStopCurrentService_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
StopCurrentService();
}

public void ShutdownService()
{
ServiceController ourService = new ServiceController("ServiceName");
if( ourService.Status == ServiceControllerStatus.Running)
{
StopCurrentService();
}
else
{
timerStopCurrentService.Start();
}
}

private void StopCurrentService()
{
ServiceController ourService = new ServiceController("ServiceName");
ourService.Stop();
}

Note : If you want to display a message box to the user use the foll.
MessageBox.Show("Error Mesage", "Application Name",
MessageBoxButtons.OK, MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
================================================== ===========================

Regards, Joy

sf*****@yahoo.com (Keith) wrote in message news:<d1**************************@posting.google. 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.public.dotnet.languages.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 ServiceController 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 discussion thread is closed

Replies have been disabled for this discussion.