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

IsServiceRunning() public static method

Determine whther the service with the given name is running.
public static IsServiceRunning ( string serviceName ) : bool
serviceName string The name of the service.
return bool
        public static bool IsServiceRunning(string serviceName)
        {
            return (GetServiceStatus(serviceName) == ServiceStatus.Running);
        }

Usage Example

コード例 #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);
        }