473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 2347
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Steele | last post by:
Okay, I assume I'm missing something obvious here. I have created a simple service in C# that on starting spawns a thread to do some processing. The service can be stopped with a "net stop" command...
2
by: Prasad | last post by:
Hi, I am writing a service which takes a long time to stop after the OnStop call is given by the Services Snap-in. The problem is I cannot cut down on the time that it takes to Stop. The Service...
4
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group :...
3
by: Timothy Shih | last post by:
Hi, I am trying to start a process from inside a .NET service. The process is a simple GUI app, the service will start up the GUI and then stop itself. The service starts the app, but no GUI...
9
by: SP | last post by:
Hi All, I wrote a windows service which is supposed to stop after specified amount of time. I am calling OnStop() after specified time. OnStop() methods executed but I dont see the service...
6
by: Jacobus Terhorst | last post by:
Using C#: I tried: ServiceController me = new ServiceController(this.ServiceName); me.Stop(); it raises an exception: Cannot find Service I also tried:
23
by: Adam Clauss | last post by:
I have a C# Windows Service running as the NetworkService account because it needs to access a network share. As part of the service's initialization, I want the service to terminate, if an...
8
by: Jerry Camel | last post by:
I want my service to terminate automatically if the specified parameters are invalid. I tried to use a servicecontroller component to attach to the service, but I think that it's failing because...
7
by: shai | last post by:
I am working at .net 1.1, writing in c#. I have windows service with a COM object. Every unexpected time The COM object throw an error that make my service get stuck (do not respond). I can catch...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.