Stopping my Windows Service Programatically

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

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
 
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.
 
Hi,

Simon Harvey said:
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?
 
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
 
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

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
 
Back
Top