"chris martin" <chris_m|NOSPAM|@caliber|SPAM|web.com> wrote in message
news:44**************************@news.easynews.co m...
|> "chris martin" <chris_m|NOSPAM|@caliber|SPAM|web.com> wrote in message
| > news:44**************************@news.easynews.co m...
| > |> You could throw an exception, and it should stop then.
| > | >
| > | > "dotnetprogrammer via DotNetMonster.com" <u14643@uwe> wrote in
| > message
| > | > news:5a82c30ba99ce@uwe...
| > | >
| > | >> I have an NT Service written in c# (.net framwork 2.0). When it
| > hits
| > | >> a
| > | >> certain error condition, I want it to be to cause itself to stop.
| > | >> How can
| > | >> I
| > | >> do this?
| > | >> -- Message posted via
http://www.dotnetmonster.com
| > | >>
| > |
| > | That's rather nasty no? How about this?:
| > |
| > | new System.ServiceProcess.ServiceController("serviceNa me").Stop();
| > |
| > |
| > Calling this will throw an "invalidoperationexception" and logs a
| > cryptic error message in the eventlog, so should not be done. You
| > better throw an exception with a description of the error, this get's
| > automatically logged in the eventlog.
| >
| > Willy.
| >
|
| Not necessarily it won't. You'll need to check if 1) the service is
running
| and 2) the service can be stopped.
|
| Otherwise the InvalidOperationException is thrown. The following works
just
| fine for me.
|
| ServiceController controller = new
System.ServiceProcess.ServiceController("Schedule" );
|
| bool isNotStopped = (controller.Status !=
ServiceControllerStatus.Stopped);
|
| if( controller.CanStop && isNotStopped)
| {
| controller.Stop();
| }
|
| Chris Martin
|
The ServiceController class is meant to 'connect/control' to existing
services, like I said it throws when you call Stop on an instance of the
(your own) running service. It works for you because "Schedule" is an
external service, but this is not what the OP asked for.
If your service needs to stop because it's no longer safe to continue, your
only option is to throw an exception with a message that gives a description
of the cause, this message will be logged in the eventlog.
Willy.