I am looking for a way to cleanly stop my service in the event of an error.
Within the worker thread, if an unrecoverable error is caught, I would like
to stop my service. Exiting the worker thread doesn't seem to do it. Using
the SCM to stop the service doesn't feel right. Am I missing something? My
service looks like (unrunnable example code below):
public partial class MyService : ServiceBase
{
protected override void OnStart(string[] args)
{
ListenerWorkerThread = new Thread(new ThreadStart(PortListener));
ListenerWorkerThread.Start();
}
protected void PortListener()
{
try
{
//do some work
}
catch (Exception ex) //some really bad error
{
//Write it to the log and stop the service cleanly
}
}
}
Kevin Yu [MSFT] - 13 Dec 2005 06:09 GMT
Hi Ben,
In .NET you can use ServiceController class to start or stop a service.
ServiceController.Stop will stop this service and any services that are
dependent on this service. Please check the following link for more
information.
http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicecontro
ller.stop.aspx
Kevin Yu

Signature
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Benj - 13 Dec 2005 18:37 GMT
Figured this out. Just needed:
this.stop();
Kevin Yu [MSFT] - 14 Dec 2005 02:35 GMT
Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.
Kevin Yu

Signature
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."