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

StopService() public static method

Stop the service with the given name. This method returns as soon as the Stop method on the service is called and does not guarantee the stopped status of the service.
public static StopService ( string serviceName ) : bool
serviceName string The name of the service
return bool
        public static bool StopService(string serviceName)
        {
            return StopService(serviceName, TimeSpan.Zero);
        }

Same methods

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

Usage Example

コード例 #1
0
        /// <summary>
        /// Uninstalls the service with the given name.
        /// </summary>
        /// <param name="name">The name of the service to uninstall.</param>
        /// <returns>True for success. Otherwise, false.</returns>
        public static bool UnInstallService(string name)
        {
            IntPtr svHandle = IntPtr.Zero;

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

                if (scHandle.ToInt64() != 0)
                {
                    int DELETE = 0x10000;
                    svHandle = OpenServiceW(scHandle, name, DELETE);

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

                    // stop the service.
                    bool running = !ServiceManager.IsServiceStopped(name);

                    if (running)
                    {
                        if (ServiceManager.StopService(name, new TimeSpan(0, 0, 0, 60)))
                        {
                            running = false;
                            Utils.Trace("{0} Service Stopped", name);
                        }
                    }

                    bool uninstalled = (DeleteService(svHandle) != 0);

                    if (uninstalled)
                    {
                        // If the service was running the delete marks the service
                        // for deletion. We need to stop the service to fully uninstall it.
                        if (running)
                        {
                            ServiceManager.StopService(name, new TimeSpan(0, 0, 0, 60));
                        }

                        return(true);
                    }

                    Utils.Trace("DeleteService Failed: {0}", name);
                    return(false);
                }
            }
            catch (Exception e)
            {
                Utils.Trace(e, "UnInstallService Exception");
            }
            finally
            {
                SafeCloseServiceHandle(svHandle);
            }

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