Home All Groups Group Topic Archive Search About

Stopping my Windows Service Programatically

Author
6 Mar 2007 12:19 PM
Simon Harvey
Hi all,

Can anybody tel me how to programatically kill my service when an
exception occurs?

Whenever something bad happens I'd like to shut it down.

Also, is there a way to programatically restart my service at a set
interval or when a particular condition is met within the application?

Many thanks

Simon

Author
6 Mar 2007 12:39 PM
DeveloperX
On 6 Mar, 12:19, Simon Harvey <notha***@hotmail.com> wrote:
> Hi all,
>
> Can anybody tel me how to programatically kill my service when an
> exception occurs?
>
> Whenever something bad happens I'd like to shut it down.
>
> Also, is there a way to programatically restart my service at a set
> interval or when a particular condition is met within the application?
>
> Many thanks
>
> Simon

I do it this way:

ServiceController controller = new
ServiceController(ConfigSettings.QueueSettings.QConfig.ServiceName);
controller.Stop();

In this case  I have the name of the service in my config as my
installer is designed to allow multiple installs on the same computer.

You can use the same controller instance to start services too.
Author
6 Mar 2007 3:31 PM
Ignacio Machin ( .NET/ C# MVP )
Hi,

"Simon Harvey" <notha***@hotmail.com> wrote in message
news:e4DFkm%23XHHA.4560@TK2MSFTNGP03.phx.gbl...
> Hi all,
>
> Can anybody tel me how to programatically kill my service when an
> exception occurs?

Catching the exception is a better idea.

> Whenever something bad happens I'd like to shut it down.

I think that treating the problem is a better solution

> Also, is there a way to programatically restart my service at a set
> interval or when a particular condition is met within the application?

What kind of problems are you seeing that is that difficult to handle?
Author
6 Mar 2007 10:08 PM
Rad [Visual C# MVP]
On Tue, 06 Mar 2007 12:19:35 +0000, Simon Harvey wrote:

> Hi all,
>
> Can anybody tel me how to programatically kill my service when an
> exception occurs?
>
> Whenever something bad happens I'd like to shut it down.
>
> Also, is there a way to programatically restart my service at a set
> interval or when a particular condition is met within the application?
>
> Many thanks
>
> Simon

The ServiceContoller class is your friend. You can use it to start and stop
services based on set criteria.

If you want to start it at timed intervals you could use a timer and within
the appropriate event write the code that invokes the service controller
Author
7 Mar 2007 1:27 PM
oscar.acostamontesde@googlemail.com
Show quote
On Mar 6, 11:08 pm, "Rad [Visual C# MVP]" <nos...@nospam.com> wrote:
> On Tue, 06 Mar 2007 12:19:35 +0000, Simon Harvey wrote:
> > Hi all,
>
> > Can anybody tel me how to programatically kill my service when an
> > exception occurs?
>
> > Whenever something bad happens I'd like to shut it down.
>
> > Also, is there a way to programatically restart my service at a set
> > interval or when a particular condition is met within the application?
>
> > Many thanks
>
> > Simon
>
> The ServiceContoller class is your friend. You can use it to start and stop
> services based on set criteria.
>
> If you want to start it at timed intervals you could use a timer and within
> the appropriate event write the code that invokes the service controller
> --
> Bits.Byteshttp://bytes.thinkersroom.com

Be aware of using class System.Timers.Timer, and not the one in
System.Windows.Forms. They dond work the same way.
Best regards
Oscar Acosta

AddThis Social Bookmark Button