471,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

How can an NT Service cause itself to stop?

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
Jan 17 '06 #1
7 2260
You could throw an exception, and it should stop then.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"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

Jan 17 '06 #2
> 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();
Jan 17 '06 #3

"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.


Jan 17 '06 #4
> "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
Jan 17 '06 #5

"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.


Jan 18 '06 #6
> "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.


Of course, you're right. :)

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

Chris
Jan 18 '06 #7
..NET 2.0 added a Stop method to ServiceBase.

http://msdn2.microsoft.com/en-us/lib...base.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
"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
Jan 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Peter Steele | last post: by
3 posts views Thread by Timothy Shih | last post: by
9 posts views Thread by SP | last post: by
6 posts views Thread by Jacobus Terhorst | last post: by
23 posts views Thread by Adam Clauss | last post: by
8 posts views Thread by Jerry Camel | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.