Programmatically stopping a Windows Service

K

Keith

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=r...=#[email protected]&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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
J

Jerry Negrelli

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
 
J

Joy

we faced a similar issue and this is what we did.
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.

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(timerStopCurrentService_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
 
K

Keith

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top