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