"chris martin" <chris_m|NOSPAM |@caliber|SPAM| web.com> wrote in message
news:44******** *************** ***@news.easyne ws.com...
|> "chris martin" <chris_m|NOSPAM |@caliber|SPAM| web.com> wrote in message
| > news:44******** *************** ***@news.easyne ws.com...
| > |> You could throw an exception, and it should stop then.
| > | >
| > | > "dotnetprogramm er via DotNetMonster.c om" <u14643@uwe> wrote in
| > message
| > | > news:5a82c30ba9 9ce@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.ServiceP rocess.ServiceC ontroller("serv iceName").Stop( );
| > |
| > |
| > Calling this will throw an "invalidoperati onexception" 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 InvalidOperatio nException is thrown. The following works
just
| fine for me.
|
| ServiceControll er controller = new
System.ServiceP rocess.ServiceC ontroller("Sche dule");
|
| bool isNotStopped = (controller.Sta tus !=
ServiceControll erStatus.Stoppe d);
|
| if( controller.CanS top && isNotStopped)
| {
| controller.Stop ();
| }
|
| Chris Martin
|
The ServiceControll er 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.