How to stop windows service from within

  • Thread starter Thread starter Vlad
  • Start date Start date
V

Vlad

I have a windows service that spawns several threads. One of those threads
may catch an exception that will require the whole service to shutdown. What
is the proper way to do it? I did some search on this subject, and the only
thing that I found is to create an instance of ServiceController and call
its Stop() method:

System.ServiceProcess.ServiceController sc = new
System.ServiceProcess.ServiceController("my Service Name");
sc.Stop();

This does not seem to work when I do it from a thread created by my service.
I'd like to avoid using System.Environment.Exit() since I am looking for a
solution that will cause OnStop() method of my service to be invoked prior
to termination of my service.

Any help would be greatly appreciated.

Vlad.
 
Hi Vlad
From within your service threads , you can't call a stop on the your
service . if you want to do some finalization code before you exit , why
don't you explicitly all the Ontsop handling function ( that is attached by
a delegate to the event ) . so you explicitly call this.onstop() before
calling System.Environment.Exit() . or you can just replicate that code
in another routine.
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
From within your service threads , you can't call a stop on the your

My intuition failed me this time.
if you want to do some finalization code before you exit , why
don't you explicitly all the Ontsop handling function ( that is attached by
a delegate to the event ) . so you explicitly call this.onstop() before
calling System.Environment.Exit(). or you can just replicate that code
in another routine.

Yep, that would work. Thanks

Vlad.
 
Back
Top