473,657 Members | 2,385 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 2357
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 void OnStart(string args) { serviceThread = new Thread(new ThreadStart(ServiceThreadStart));...
2
2844
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 around this problem. Thanks Prasad
4
15881
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
3047
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 i run the application by double clicking on it, the GUI appears fine. Here is the code: Process...
9
7252
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
28532
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
7576
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 thrown saying access denied. If I switch to using the LocalService account it works fine, but I lose...
8
1421
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 within the service itself? Thanks. Jerry
7
7222
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 stop and start my service. But there is one thing I do not understand:
0
8420
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6176
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2743
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
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.