By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,078 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,078 IT Pros & Developers. It's quick & easy.

How to stop a Windows Service from within itself?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
:)

--

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.