How do you stop a Window Service

S

srcleveland

I am trying to have a windows service stop itself. I am currently
using the following code but I would like to find a way to stop the
service without having to shell out to a command prompt

Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.Arguments = " /C sc stop \"" + this.serviceDisplayName +
"\"";
p.Start();
p.WaitForExit();

One of the drawbacks of the above code is that you have to know the
name under which the service is running in order to be able to stop it.
 
M

Mike Labosh

I am trying to have a windows service stop itself. I am currently
using the following code but I would like to find a way to stop the
service without having to shell out to a command prompt

If it needs to stop itself, can't you just have this service call it's own
overridden Stop() method?
 
P

PS

I am trying to have a windows service stop itself. I am currently
using the following code but I would like to find a way to stop the
service without having to shell out to a command prompt

Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.Arguments = " /C sc stop \"" + this.serviceDisplayName +
"\"";
p.Start();
p.WaitForExit();

One of the drawbacks of the above code is that you have to know the
name under which the service is running in order to be able to stop it.

So you are wanting to stop the name of a service when you don't know it's
name, is that correct?

You can use the ServiceController class however you will still need to know
the name of the service.

PS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top