This project has moved. For the latest updates, please go here.

Stopping and Restarting openPDC Service programatically

Jan 14, 2013 at 1:43 PM

I have a program that monitors the openPDC, and needs to stop/restart the  openPDC Service in some cases. Currently I am doing this  via the 'net start' and 'net stop' command-line commands. I find this a poor option, but cannot figure out how to mimic the  openPDC Manager's 'Restart openPDC' command.

Is this function available to external applications (.NET/C#)? If so, how is it used?

Coordinator
Jan 14, 2013 at 1:58 PM
string serviceName = "openPDC";
                // Attempt to access service controller for the specified Windows service
                ServiceController serviceController = ServiceController.GetServices().SingleOrDefault(svc => string.Compare(svc.ServiceName, serviceName, true) == 0);
 
                if (serviceController != null)
                {
                    try
                    {
                        if (serviceController.Status == ServiceControllerStatus.Running)
                        {
                            System.Console.WriteLine("Attempting to stop the {0} Windows service...", serviceName);
 
                            serviceController.Stop();
 
                            // Can't wait forever for service to stop, so we time-out after 20 seconds
                            serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(20.0D));
 
                            if (serviceController.Status == ServiceControllerStatus.Stopped)
                                System.Console.WriteLine("Successfully stopped the {0} Windows service.", serviceName);
                            else
                                System.Console.WriteLine("Failed to stop the {0} Windows service after trying for 20 seconds...", serviceName);
 
                            // Add an extra line for visual separation of service termination status
                            System.Console.WriteLine("");
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine("Failed to stop the {0} Windows service: {1}\r\n", serviceName, ex.Message);
                    }
                }
 
Jan 14, 2013 at 3:52 PM

thanks so much!!! this works fine, although for some reason 20 seconds, even 30 seconds didn't work. But 40 seconds seems adequate, so I used 90 seconds to be sure.

Coordinator
Jan 14, 2013 at 7:46 PM

Here's the full restart code we use - perhaps it just kills the service more often than I'm aware:

                // Attempt to access service controller for the specified Windows service
                ServiceController serviceController = ServiceController.GetServices().SingleOrDefault(svc => string.Compare(svc.ServiceName, serviceName, true) == 0);
 
                if (serviceController != null)
                {
                    try
                    {
                        if (serviceController.Status == ServiceControllerStatus.Running)
                        {
                            System.Console.WriteLine("Attempting to stop the {0} Windows service...", serviceName);
 
                            serviceController.Stop();
 
                            // Can't wait forever for service to stop, so we time-out after 20 seconds
                            serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(20.0D));
 
                            if (serviceController.Status == ServiceControllerStatus.Stopped)
                                System.Console.WriteLine("Successfully stopped the {0} Windows service.", serviceName);
                            else
                                System.Console.WriteLine("Failed to stop the {0} Windows service after trying for 20 seconds...", serviceName);
 
                            // Add an extra line for visual separation of service termination status
                            System.Console.WriteLine("");
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine("Failed to stop the {0} Windows service: {1}\r\n", serviceName, ex.Message);
                    }
                }
 
                // If the service failed to stop or it is installed as stand-alone debug application, we try to forcibly stop any remaining running instances
                try
                {
                    Process[] instances = Process.GetProcessesByName(serviceName);
 
                    if (instances.Length > 0)
                    {
                        int total = 0;
                        System.Console.WriteLine("Attempting to stop running instances of the {0}...", serviceName);
 
                        // Terminate all instances of service running on the local computer
                        foreach (Process process in instances)
                        {
                            process.Kill();
                            total++;
                        }
 
                        if (total > 0)
                            System.Console.WriteLine(string.Format("Stopped {0} {1} instance{2}.", total, serviceName, total > 1 ? "s" : ""));
 
                        // Add an extra line for visual separation of process termination status
                        System.Console.WriteLine("");
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine("Failed to terminate running instances of the {0}{1}\r\n", serviceName, ex.Message);
                }
 
                // Attempt to restart Windows service...
                if (serviceController != null)
                {
                    try
                    {
                        // Refresh state in case service process was forcibly stopped
                        serviceController.Refresh();
 
                        if (serviceController.Status != ServiceControllerStatus.Running)
                            serviceController.Start();
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine("Failed to restart the {0} Windows service: {1}\r\n", serviceName, ex.Message);
                    }
                }
Jan 15, 2013 at 1:50 PM

thanks...