473,406 Members | 2,707 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,406 software developers and data experts.

How to stop a Windows Service from within itself?

Using C#:

I tried:
ServiceController me = new ServiceController(this.ServiceName);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start("net.exe", "stop " + this.ServiceName);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst

Nov 17 '05 #1
6 28388
Throw an unhandled exception and your service should stop.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Jacobus Terhorst" <ja*****@usa.net> wrote in message
news:Os*************@TK2MSFTNGP15.phx.gbl...
Using C#:

I tried:
ServiceController me = new ServiceController(this.ServiceName);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start("net.exe", "stop " + this.ServiceName);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst

Nov 17 '05 #2
:)

--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

"John Timney (ASP.NET MVP)" <ti*****@despammed.com> wrote in message
news:ed*************@TK2MSFTNGP15.phx.gbl...
Throw an unhandled exception and your service should stop.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Jacobus Terhorst" <ja*****@usa.net> wrote in message
news:Os*************@TK2MSFTNGP15.phx.gbl...
Using C#:

I tried:
ServiceController me = new ServiceController(this.ServiceName);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start("net.exe", "stop " + this.ServiceName);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst


Nov 17 '05 #3
I tried it, but no cigar.

I am starting a timer in the OnStart method

and the worker code is started from the timer event.

Wherever I throw an unhandled exception, the service is not stopped...

"John Timney (ASP.NET MVP)" <ti*****@despammed.com> wrote in message
news:ed*************@TK2MSFTNGP15.phx.gbl...
Throw an unhandled exception and your service should stop.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Jacobus Terhorst" <ja*****@usa.net> wrote in message
news:Os*************@TK2MSFTNGP15.phx.gbl...
Using C#:

I tried:
ServiceController me = new ServiceController(this.ServiceName);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start("net.exe", "stop " + this.ServiceName);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst


Nov 17 '05 #4
It does work from the OnStart method...
"Jacobus Terhorst" <ja*****@usa.net> wrote in message
news:eP**************@TK2MSFTNGP10.phx.gbl...
I tried it, but no cigar.

I am starting a timer in the OnStart method

and the worker code is started from the timer event.

Wherever I throw an unhandled exception, the service is not stopped...

"John Timney (ASP.NET MVP)" <ti*****@despammed.com> wrote in message
news:ed*************@TK2MSFTNGP15.phx.gbl...
Throw an unhandled exception and your service should stop.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Jacobus Terhorst" <ja*****@usa.net> wrote in message
news:Os*************@TK2MSFTNGP15.phx.gbl...
Using C#:

I tried:
ServiceController me = new ServiceController(this.ServiceName);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start("net.exe", "stop " + this.ServiceName);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst



Nov 17 '05 #5
Where are you trying to stop the service from? I don't think that you can
stop it from the initialization or start method in the way that you are
describing.

--
Jared Parsons [MSFT]
ja******@online.microsoft.com
http://blogs.msdn.com/jaredpar
"This posting is provided "AS IS" with no warranties, and confers no rights"
"Jacobus Terhorst" <ja*****@usa.net> wrote in message
news:Os*************@TK2MSFTNGP15.phx.gbl...
Using C#:

I tried:
ServiceController me = new ServiceController(this.ServiceName);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start("net.exe", "stop " + this.ServiceName);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst

Nov 17 '05 #6
Have you tried a simple Application.Exit()

You might have to call exitthread before calling exit.

The service itself must have the CanStop property set to true when your
developing the service. You can check if you can stop it with .......
if (myController.CanStop)
{
myController.Stop();
}
The suggestion to throw an exception is a complete hack that another MVP
suggested worked. The reason it works in onStart is that the service has
not yet started.

Your alternative would be to overide the DACL for the service, allocating a
new security descriptor and then using a call to advapi32 to get a better
control at the API level. It still might not work, as the user account your
service runs under has to have service stop permissions so check which
account yout running the service under.

-- Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
"Jacobus Terhorst" <ja*****@usa.net> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
It does work from the OnStart method...
"Jacobus Terhorst" <ja*****@usa.net> wrote in message
news:eP**************@TK2MSFTNGP10.phx.gbl...
I tried it, but no cigar.

I am starting a timer in the OnStart method

and the worker code is started from the timer event.

Wherever I throw an unhandled exception, the service is not stopped...

"John Timney (ASP.NET MVP)" <ti*****@despammed.com> wrote in message
news:ed*************@TK2MSFTNGP15.phx.gbl...
Throw an unhandled exception and your service should stop.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Jacobus Terhorst" <ja*****@usa.net> wrote in message
news:Os*************@TK2MSFTNGP15.phx.gbl...
Using C#:

I tried:
ServiceController me = new ServiceController(this.ServiceName);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start("net.exe", "stop " + this.ServiceName);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst




Nov 17 '05 #7

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

Similar topics

2
by: Simon Niederberger | last post by:
Hi I've written a Windows Service which has - several (0-100) listeners threads which spawn worker threads based on events, timers etc - several (0-300) worker threads which handle data...
5
by: Paul O. Morris | last post by:
Is there a script that I can run to stop a particular SQL server service on Win2003 server? I'm looking for a similar script to restart that service as well. Thanks.
1
by: Mike001 | last post by:
Hi I have developed a Windows service in C# (inherited from System.ServiceProcess.ServiceBase) and, after the service has successfully started, when I catch an exception I'd like to write the code...
13
by: Åženol Akbulak | last post by:
Hi, I have an asp.net application. And I have worker threads which must run always (7x24). I start my threads in Application_Start, and I stop its in Application_Stop events. I log this...
2
by: Nathan | last post by:
I have created a windows service but am having difficulties handling exceptions in the OnStart. What I want to do is if an exeception occurs during any of the code executed during the OnStart is...
4
by: Blaxer | last post by:
I have read approximately 30 articles now on various methods to make your vb.net application automatically update itself and I can't see how they apply to a vb.net windows services projects. The...
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...
2
by: Cappamontana | last post by:
Hi, We have a .Net project containing 1 Windows Application and 3 Windows services. The project is quite complex and is still in development. The services can be started and stopped by using the...
8
by: Lemune | last post by:
Hi, I'm developing window service application on C# 2005. The service is to read a new excel file on certain directory, and process it to database. The service work find on XP. But when I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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,...
0
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...

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.