views:

353

answers:

2

Hi,

I've a .Net windows service which is on App server 1. From this service, I need to programmatically start and stop another .Net windows service sitting on App server 2. How do I achieve this ?

Thanks for reading.

+2  A: 

Use the ServiceController class

Sam Saffron
thanks for ur answer...can i also remotely control a windows service using this class?
Steve Chapman
sure, http://msdn.microsoft.com/en-us/library/ssbk2tf3.aspx
Sam Saffron
+1  A: 

This is an outline of what you will need to do.

System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController();
sc.ServiceName = "service name";
sc.MachineName = ".";// for local.  use windows machine name here for a remote service
sc.Start();
TimeSpan timeout = new TimeSpan(0, 0, 0, 3, 0); // 3 sec
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, ts);
if (sc.Status == System.ServiceProcess.ServiceControllerStatus.Running)
    Console.WriteLine("started");
else
    Console.WriteLine("failed to start");
Kleinux
thanks for ur inputs!
Steve Chapman