How can an NT Service cause itself to stop?

  • Thread starter dotnetprogrammer via DotNetMonster.com
  • Start date
D

dotnetprogrammer via DotNetMonster.com

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?
 
C

chris martin

You could throw an exception, and it should stop then.
That's rather nasty no? How about this?:

new System.ServiceProcess.ServiceController("serviceName").Stop()
 
W

Willy Denoyette [MVP]

|> You could throw an exception, and it should stop then.
| >
| > | >
| >> 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("serviceName").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.
 
C

chris martin

chris martin said:
|> You could throw an exception, and it should stop then.
| >
message
| > | >
| >> 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("serviceName").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
 
W

Willy Denoyette [MVP]

| > | > |> You could throw an exception, and it should stop then.
| > | >
| > message
| > | > | > | >
| > | >> 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("serviceName").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.
 
C

chris martin

chris martin said:
message
| > | > |> You could throw an exception, and it should stop then.
| > | >
| > message
| > | > | > | >
| > | >> 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("serviceName").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.

Of course, you're right. :)

I totally missed that he needed to stop the currently running service.

Chri
 
J

Jay B. Harlow [MVP - Outlook]

..NET 2.0 added a Stop method to ServiceBase.

http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase.stop.aspx

You can use it to stop the currently executing service.


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|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
 

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