views:

75

answers:

3

I am a complete beginner to working with windows services. I have a basic skeleton worked out for the service and I am currently doing this:

protected override void OnStart(string[] args)
    {
        base.OnStart(args);
        Process.Start(@"someProcess.exe");
    }

just to fire-off the exe at the start of the program.

However, I'd like to have the service stop itself when the process started from the exe quits. I'm pretty sure I need to do some sort of threading (something I am also a beginner with), but I'm not sure of the overall outline of how this works, nor the specific way to stop a process from within itself. Could you help me with the general process for this (i.e. start a thread from OnStart, then what...?)? Thanks.

A: 

someProcess.exe should have someLogic to stop the calling service ;)

Use the ServiceController class.

// Toggle the Telnet service - 
// If it is started (running, paused, etc), stop the service.
// If it is stopped, start the service.
ServiceController sc = new ServiceController("Telnet");
Console.WriteLine("The Telnet service status is currently set to {0}", 
                  sc.Status.ToString());

if  ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
     (sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
   // Start the service if the current status is stopped.

   Console.WriteLine("Starting the Telnet service...");
   sc.Start();
}  
else
{
   // Stop the service if its status is not set to "Stopped".

   Console.WriteLine("Stopping the Telnet service...");
   sc.Stop();
}  

// Refresh and display the current service status.
sc.Refresh();
Console.WriteLine("The Telnet service status is now set to {0}.", 
                   sc.Status.ToString());

Code is from the page linked above.

Pierre 303
Thanks, this does look like it would accomplish the end result - if I am developing the exe aswell. Isn't this more of a workaround than a solution, though? And it would only work assuming I'm writing the .exe. Assume I don't have access to the source for the exe, and this should still be do-able through the windows service itself.
roviuser
+3  A: 

You can use a BackgroundWorker for the threading, use Process.WaitForExit() to wait for the process to terminate until you stop your service.

You're right that you should do some threading, doing lots of work in the OnStart may render errors about not starting correctly from Windows when starting the service.

protected override void OnStart(string[] args)
{

    BackgroundWorker bw = new BackgroundWorker();
    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    bw.RunWorkerAsync();
}

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    Process p = new Process();
    p.StartInfo = new ProcessStartInfo("file.exe");
    p.Start();
    p.WaitForExit();
    base.Stop();
}

Edit You may also want to move the Process p to a class member and stop the process in the OnStop to make sure that you can stop the service again if the exe goes haywire.

protected override void OnStop()
{
    p.Kill();
}
Albin Sunnanbo
Thanks, this worked pretty much exactly as I'd hoped.
roviuser
+1  A: 

You have to use a ServiceController to do it, it has a Stop method. Make sure your service has the CanStop property set to true.

James