473,573 Members | 2,874 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2355
You could throw an exception, and it should stop then.

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

"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

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

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


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

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


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


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
"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
Jan 26 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
363
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 of course, but under some circumstances the service will decide to terminate itself. My OnStart looks something like this: protected override...
2
2832
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 snap-in gives me the error saying that the service did not respond to the Stop call in a timely fashion. So is there any method by which I can get...
4
15873
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 : microsoft.public.dotnet.languages.csharp URL :...
3
3046
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 appears. However, I can see the application running in the task manager. I made sure that CreateNoWindow was set to false and still no GUI appears. If...
9
7245
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 stopping. Please advise how to stop the service. Thanks, SP
6
28490
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
7564
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 unrecoverable error occurs. When that case occurs, I create a ServiceController object and call the Stop() method. However - I get an exception...
8
1419
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 it's being called in the OnStart event (where I validate the parameters) and the service isn't fully running yet. How can I stop my service from...
7
7219
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 this error. I want to restart my windows service every time the COM object throws an error. I use System.ServiceProcess.ServiceController to...
0
7767
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8006
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8058
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6400
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5579
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5288
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3729
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3729
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2191
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.