Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

How to shut down my windows service

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mike - 11 Aug 2005 19:36 GMT
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
Lee Gillie - 11 Aug 2005 19:42 GMT
> Hello.
>
[quoted text clipped - 6 lines]
>
> 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.
Erick Sgarbi - 11 Aug 2005 22:47 GMT
> 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

> Hello.
>
[quoted text clipped - 6 lines]
>
> Mike
Lee Gillie - 11 Aug 2005 23:13 GMT
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
Erick Sgarbi - 11 Aug 2005 23:50 GMT
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?

--
Erick Sgarbi
www.blog.csharpbox.com

> Erick -
>
[quoted text clipped - 7 lines]
>
> - Lee
mike - 12 Aug 2005 13:12 GMT
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

> Sure, my post was just an addendum to yours. I am not sure either since he
> emphasized on "UI" button behavior.
[quoted text clipped - 15 lines]
>>
>> - Lee
Lee Gillie - 12 Aug 2005 15:39 GMT
> 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
mike - 12 Aug 2005 18:53 GMT
Hi, Lee. Thanks for the response.

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

>> I'm still not sure how to do this.
>
[quoted text clipped - 33 lines]
>
> - Lee
Lee Gillie - 16 Aug 2005 06:04 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.