Opc.Ua.Configuration.ServiceManager.StartService C# (CSharp) Method

StartService() public static method

Start the service with the given name. This method returns as soon as the Start method on the service is called and does not guarantee the running status of the service. You can call this method after stop or pause the service in order to re-start it.
public static StartService ( string serviceName ) : bool
serviceName string The name of the service
return bool
        public static bool StartService(string serviceName)
        {
            return StartService(serviceName, TimeSpan.Zero);
        }

Same methods

ServiceManager::StartService ( string serviceName, System.TimeSpan timeout ) : bool

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Stops the service.
        /// </summary>
        /// <param name="serviceName">Name of the service.</param>
        /// <returns>True if stopped successfully.</returns>
        public static bool StartService(string serviceName)
        {
            IntPtr svHandle = IntPtr.Zero;

            try
            {
                IntPtr scHandle = OpenSCManagerW(null, null, (uint)ServiceAccess.GenericWrite);

                if (scHandle.ToInt64() != 0)
                {
                    svHandle = OpenServiceW(scHandle, serviceName, (int)ServiceAccess.AllAccess);

                    if (svHandle.ToInt64() == 0)
                    {
                        Utils.Trace("OpenService Error: {0}", GetLastError());
                        return(false);
                    }

                    // stop the service.
                    bool running = ServiceManager.IsServiceRunning(serviceName);

                    if (!running)
                    {
                        if (ServiceManager.StartService(serviceName, new TimeSpan(0, 0, 0, 60)))
                        {
                            Utils.Trace("{0} Service Started", serviceName);
                            return(true);
                        }
                    }

                    return(false);
                }
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error stopping service.");
            }
            finally
            {
                SafeCloseServiceHandle(svHandle);
            }

            return(false);
        }
All Usage Examples Of Opc.Ua.Configuration.ServiceManager::StartService