How to shut down my windows service

  • Thread starter Thread starter mike
  • Start date Start date
M

mike

Hello.

I've created a windows service application. I am also creating a UI for it.

I want to add a "Stop Service" button in my UI app, but am unsure how to
code the shut down within the service app. Call Dispose()?

Thanks in advance,

Mike
 
mike said:
Hello.

I've created a windows service application. I am also creating a UI for it.

I want to add a "Stop Service" button in my UI app, but am unsure how to
code the shut down within the service app. Call Dispose()?

Thanks in advance,

Mike

In the OnStop event quit doing what you are doing.
Close files. Stop listening. Stop timers. Exit loops. Etc.
You probably created a main process thread in the OnStart
event handler. Cause that thread to stop.

Then when you return from OnStop the service process exits.
 
I want to add a "Stop Service" button in my UI app, but am unsure how
to
code the shut down within the service app. Call Dispose()?
Look at service controller:

ServiceController controller = new ServiceController("MyService");
controller.Start();
controller.Stop();


Don't forget to reference System.ServiceProcess.

HTH
Erick Sgarbi
www.blog.csharpbox.com
 
Erick -

Just so as not to confuse Mike....

The code you provided would reside in UI app.
Mike asks what needs to be done in the SERVICE APP.
"but am unsure how to
code the shut down within
the service app"

- Lee
 
Sure, my post was just an addendum to yours. I am not sure either since
he emphasized on "UI" button behavior.
Mike, did our posts help?
 
Hi Erick.

They helped somewhat. Lee was right, I need to shut down the service from
the service app.

I'm still not sure how to do this.

Thanks,

Mike
 
mike said:
I'm still not sure how to do this.

Mike -

With the .NET Framework you implement method overrides in your class
which are inherited from the ServiceBase class of the framework. The
..NET wizard created this class for you.

OnStart and OnStop are about a minimum for you to override/implement.
The service control manager (SCM or "scum") loads your service EXE and
invokes your OnStart (basically). Then in response to a request to stop
the service it will invoke your OnStop override. When you return from
THIS call, your service executable exits, and the process goes away.

The work of your service is NOT performed in OnStart. You must get your
service started up and running in OnStart, and then return to caller as
soon as you can. Typically one would create a thread which is sometimes
a loop which performs your service activity. Or perhaps you may start a
timer. Or use some other system component which has a callback for you
to respond to.

When responding to OnStop you undo what you started in your OnStart. You
cause your service thread to exit. You exit the loop. Stop the timer. Or
dispose the other system component which is making call backs for you.
In any case, be sure everything is shutdown cleanly before you exit the
OnStop override in your implementation. But when you exit the OnStop
method, the executable exits and your process is gone. Consider what
your service is "waiting for" or "watching to happen" in its primary
function. Often times the mechanism needs to addtionally use a
ManualResetEvent, or something similar, so your main "wait for
something" can way for the MRE as well. Then in OnStop set the MRE to
trigger the service thread to exit. Typically you need a little extra
infrastructure to support stopping the primary activity in this way.

HTH

- Lee
 
Hi, Lee. Thanks for the response.

How would I shut down the service during the OnStart event?
 
mike said:
Hi, Lee. Thanks for the response.

How would I shut down the service during the OnStart event?

How to FAIL to START you mean?

You could throw an exception, allowing it to be logged, which will leave
some tracks. If you wanted to be RUDE you could simply exit the image.

- Lee
 
Back
Top