当前位置: 首页>>代码示例>>C#>>正文


C# ServiceController.Start方法代码示例

本文整理汇总了C#中System.ServiceProcess.ServiceController.Start方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceController.Start方法的具体用法?C# ServiceController.Start怎么用?C# ServiceController.Start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.ServiceProcess.ServiceController的用法示例。


在下文中一共展示了ServiceController.Start方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: StartService

        /// <summary>
        /// Start the service with the given name and wait until the status of the service is running.
        /// If the service status is not running after the given timeout then the service is considered not started.
        /// You can call this method after stop or pause the service in order to re-start it.
        /// </summary>
        /// <param name="serviceName">The name of the service</param>
        /// <param name="timeout">The timeout.</param>
        /// <returns>True if the service has been started. Otherwise, false.</returns>
        public static bool StartService(string serviceName, TimeSpan timeout)
        {
            try
            {
                bool timeoutEnabled = (timeout.CompareTo(TimeSpan.Zero) > 0);
                using (ServiceController c = new ServiceController(serviceName))
                {
                    c.Refresh();
                    if (timeoutEnabled && c.Status == ServiceControllerStatus.Running)
                        return true;
                    if (!timeoutEnabled && (c.Status == ServiceControllerStatus.Running || c.Status == ServiceControllerStatus.StartPending || c.Status == ServiceControllerStatus.ContinuePending))
                        return true;

                    if (c.Status == ServiceControllerStatus.Paused || c.Status == ServiceControllerStatus.ContinuePending)
                        c.Continue();
                    else if (c.Status == ServiceControllerStatus.Stopped || c.Status == ServiceControllerStatus.StartPending)
                        c.Start();
                    if (timeoutEnabled)
                        c.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    return true;
                }
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error starting service {0}.", serviceName);
                return false;
            }
        }
开发者ID:OPCFoundation,项目名称:UA-.NET,代码行数:36,代码来源:ServiceManager.cs

示例2: RestartSB

            public static void RestartSB()
            {
                string ServiceName = "SolutionBuilder Core Service";

                ServiceController service = new ServiceController();
                service.MachineName = ".";
                service.ServiceName = ServiceName;
                string status = service.Status.ToString();
                try
                {
                    if (service.Status == ServiceControllerStatus.Running)
                    {
                        service.Stop();
                        service.WaitForStatus(ServiceControllerStatus.Stopped);
                    }
                    //Recycle App Pool
                    try
                    {
                        RecycleAppPool();
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                    if (service.Status == ServiceControllerStatus.Stopped)
                    {
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running);
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }
开发者ID:iSmartCon,项目名称:ToolBox,代码行数:35,代码来源:Plugin.cs

示例3: serviceStart

        private static void serviceStart()
        {
            ServiceController[] scServices;
            scServices = ServiceController.GetServices();

            foreach (ServiceController scTemp in scServices)
            {

                if (scTemp.ServiceName == "open-audit-check-service")
                {

                    ServiceController sc = new ServiceController("open-audit-check-service");
                    if (sc.Status == ServiceControllerStatus.Stopped)
                    {
                        int tries = 0;
                        sc.Start();
                        while (sc.Status == ServiceControllerStatus.Stopped && tries < 5)
                        {
                            Thread.Sleep(1000);
                            sc.Refresh();
                            tries++;
                        }
                    }

                }
            }
        }
开发者ID:damico,项目名称:open-audit,代码行数:27,代码来源:OpenAuditCheckService.cs

示例4: InstallService

 /// <summary>
 /// 安装服务:
 /// </summary>
 /// <param name="filepath"></param>
 public void InstallService(string filepath)
 {
     try
     {
         string serviceName = GetServiceName(filepath);
         ServiceController service = new ServiceController(serviceName);
         if (!ServiceIsExisted(serviceName))
         {
             //Install Service
             AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller() { UseNewContext = true, Path = filepath };
             myAssemblyInstaller.Install(new Hashtable());
             myAssemblyInstaller.Commit(new Hashtable());
             myAssemblyInstaller.Dispose();
             //--Start Service
             service.Start();
         }
         else
         {
             if (service.Status != ServiceControllerStatus.Running && service.Status != ServiceControllerStatus.StartPending)
             {
                 service.Start();
             }
         }
     }
     catch (Exception ex)
     {
         throw new Exception("installServiceError/n" + ex.Message);
     }
 }
开发者ID:yangdaichun,项目名称:ZHXY.ZSXT,代码行数:33,代码来源:ManageService.cs

示例5: MonitorServiceStart

        public void MonitorServiceStart()
        {
            ServiceController cs = new ServiceController();
            ServiceController cgw = new ServiceController();
            try
            {
                cs.ServiceName = "HUAWEI SMC 2.0 MonitorManage";
                cs.Refresh();

                cgw.ServiceName = "HUAWEI SMC 2.0 ConvergeGateway";
                cgw.Refresh();
                if (cgw.Status == ServiceControllerStatus.Running || cgw.Status == ServiceControllerStatus.StartPending)  //监控服务自启动的前提是CGW服务在线
                {
                    //if (cs.Status != ServiceControllerStatus.Running && cs.Status != ServiceControllerStatus.StartPending)
                    if (cs.Status == ServiceControllerStatus.Stopped)
                    {
                        //Thread.Sleep(1000);
                        TimeSpan timeout = TimeSpan.FromMilliseconds(CgwConst.WAIT_MONITOR_SERVICE_RUNNING_MILLI_SECONDS);
                        cs.Start();
                        cs.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    }
                }
            }
            catch (System.Exception)
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(CgwConst.WAIT_MONITOR_SERVICE_RUNNING_MILLI_SECONDS);
                cs.Start();
                cs.WaitForStatus(ServiceControllerStatus.Running, timeout);
            }
        }
开发者ID:eSDK,项目名称:esdk_Cgw,代码行数:30,代码来源:MonitorServiceControl.cs

示例6: RestartWindowsService

        public static void RestartWindowsService(string machiname, string serviceName, int timeoutMilliseconds)
        {
            ServiceController service = new ServiceController(serviceName, machiname);
            TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

            try
            {
                if ((service.Status.Equals(ServiceControllerStatus.Stopped)) || (service.Status.Equals(ServiceControllerStatus.StopPending)))
                {
                    service.Start();
                }
                else
                {
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running, timeout);

                }
                MessageBox.Show("Restart yapıldı");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
开发者ID:GokhanBozkurt,项目名称:MyRepository,代码行数:27,代码来源:SearchText.cs

示例7: StartService

        public static string StartService(string servicename, string[] parameters = null)
        {
            ServiceController sc = new ServiceController(servicename);

            if (sc.Status == ServiceControllerStatus.Running) return "Running";

            try
            {
                if (parameters != null)
                {
                    sc.Start(parameters);
                }
                else
                {
                    sc.Start();
                }

                return CheckServiceCondition(servicename);
            }
            catch (Exception e)
            {
                //Service can't start because you don't have rights, or its locked by another proces or application
                return e.Message;
            }
        }
开发者ID:NewAmbition,项目名称:ServiceNecromancer,代码行数:25,代码来源:Scrolls.cs

示例8: Main

		static void Main(string[] args)
		{
			(new Logger()).WriteNotice("IIS restarting: begin");
			try
			{
				System.Diagnostics.Process cIISreset = new System.Diagnostics.Process();
				cIISreset.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\iisreset.exe";
				cIISreset.Start();
				(new Logger()).WriteDebug("iisreset.exe started...");
				System.Diagnostics.Process[] aPPP;
				while (true)
				{
					System.Threading.Thread.Sleep(1000);
					aPPP = System.Diagnostics.Process.GetProcesses();
					if (null == aPPP.FirstOrDefault(o => o.ProcessName == "iisreset"))
						break;
				}
				(new Logger()).WriteNotice("IIS restarted");
			}
			catch (Exception ex)
			{
				(new Logger()).WriteError(ex);
			}

			(new Logger()).WriteNotice("IG restarting: begin");
			try
			{
				ServiceController controller = new ServiceController();
				controller.ServiceName = "InGenie.Initiator"; // i.e “w3svc”
				if (controller.Status != ServiceControllerStatus.Running)
				{
					controller.Start();
				}
				else
				{
					controller.Stop();
					(new Logger()).WriteDebug("InGenie.Initiator stopping...");
					controller.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10));
					System.Threading.Thread.Sleep(1000);
					(new Logger()).WriteDebug("InGenie.Initiator stopped...");
					controller.Start();
				}
				(new Logger()).WriteDebug("InGenie.Initiator starting...");
				controller.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
				(new Logger()).WriteNotice("InGenie.Initiator restarted");
			}
			catch (Exception ex)
			{
				(new Logger()).WriteError(ex);
			}
			System.Threading.Thread.Sleep(1000);
		}
开发者ID:ratsil,项目名称:bethe.ingenie,代码行数:52,代码来源:Restart.cs

示例9: runServiceCheck

        public void runServiceCheck()
        {
            Utils util = new Utils();
            ServiceController sc = null;
            try
            {
                sc = new ServiceController(serviceName);

                switch (sc.Status)
                {
                    case ServiceControllerStatus.Running:
                        break;
                    case ServiceControllerStatus.Stopped:
                        sc.Start();
                        util.writeToLogFile("service checker starting "+serviceName);
                        break;
                    case ServiceControllerStatus.Paused:
                        sc.Stop();
                        sc.Start();
                        util.writeToLogFile("service checker starting " + serviceName);
                        break;
                    case ServiceControllerStatus.StopPending:
                        sc.Stop();
                        sc.Start();
                        util.writeToLogFile("service checker starting " + serviceName);
                        break;
                    case ServiceControllerStatus.StartPending:
                        sc.Stop();
                        sc.Start();
                        util.writeToLogFile("service checker starting " + serviceName);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                util.writeEventLog(e.Message);
                util.writeEventLog(e.StackTrace);
            }
            finally
            {
                try
                {
                    if (sc != null) sc.Close();

                } catch (Exception) {}
            }
        }
开发者ID:damico,项目名称:open-audit,代码行数:49,代码来源:CheckServiceThreadImpl.cs

示例10: Restart

        public static void Restart()
        {
            ServiceController service = new ServiceController("Spooler");
            if ((service.Status.Equals(ServiceControllerStatus.Stopped)) || (service.Status.Equals(ServiceControllerStatus.StopPending)))
            {
                service.Start();
            }
            else
            {
                service.Stop();

                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running);
            }
        }
开发者ID:liorg,项目名称:DesignPatternsForXrm,代码行数:15,代码来源:PrinterQuery.cs

示例11: Execute

        public override void Execute(HostArguments args)
        {
            if (!ServiceUtils.IsServiceInstalled(args.ServiceName))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Out.WriteLine("The '{0}' service is not installed.", args.ServiceName);
                Console.ResetColor();

                return;
            }

            var stopController = new ServiceController(args.ServiceName);

            if (stopController.Status == ServiceControllerStatus.Running)
            {
                stopController.Stop();
                stopController.WaitForStatus(ServiceControllerStatus.Stopped);
            }
            if (stopController.Status != ServiceControllerStatus.Running)
            {
                stopController.Start();
                stopController.WaitForStatus(ServiceControllerStatus.Running);
            }

            Console.Out.WriteLine("Service restarted");    
        }
开发者ID:dmitriylyner,项目名称:ServiceControl,代码行数:26,代码来源:RestartCommand.cs

示例12: Main

        /// <summary>
        /// The main entry point for the service.
        /// </summary>
        static void Main(string[] args)
        {
            if (System.Environment.UserInteractive)
            {
                try
                {
                    // Arguments used by the installer
                    ServiceController controller;
                    string parameter = string.Concat(args);
                    switch (parameter)
                    {
                        case "--install":
                            ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                            controller = new ServiceController("Wev HTTP Listener");
                            controller.Start();
                            break;

                        case "--uninstall":
                            controller = new ServiceController("Wev HTTP Listener");
                            controller.Stop();
                            ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                            break;
                    }
                }
                catch (Exception) { }
            }
            else
            {
                ServiceBase.Run(new WevService());
            }
        }
开发者ID:yetanotherchris,项目名称:wev,代码行数:34,代码来源:Program.cs

示例13: StartService

        public static int StartService(string strServiceName,
out string strError)
        {
            strError = "";

            ServiceController service = new ServiceController(strServiceName);
            try
            {
                TimeSpan timeout = TimeSpan.FromMinutes(2);

                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);
            }
            catch (Exception ex)
            {
                if (GetNativeErrorCode(ex) == 1060)
                {
                    strError = "服务不存在";
                    return -1;
                }

                else if (GetNativeErrorCode(ex) == 1056)
                {
                    strError = "调用前已经启动了";
                    return 0;
                }
                else
                {
                    strError = ExceptionUtil.GetAutoText(ex);
                    return -1;
                }
            }

            return 1;
        }
开发者ID:renyh1013,项目名称:dp2,代码行数:35,代码来源:InstallHelper.cs

示例14: serviceInstaller1_AfterInstall

 private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
 {
     using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
     {
         sc.Start();
     }
 }
开发者ID:Beterer,项目名称:tnLabs,代码行数:7,代码来源:ProjectInstaller.cs

示例15: RestartMemCache

        private static void RestartMemCache()
        {
            // Memcached service
            using (ServiceController memCacheService = new ServiceController("memcached Server"))
            {
                if (memCacheService != null && memCacheService.Container != null)
                {
                    TimeSpan timeout = TimeSpan.FromMilliseconds(10000);

                    // Stop the memcached service
                    if (memCacheService.CanStop)
                    {
                        memCacheService.Stop();
                        memCacheService.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                    }

                    // start memcached service
                    if (memCacheService.Status != ServiceControllerStatus.Running)
                    {
                        memCacheService.Start();
                        memCacheService.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    }
                }
            }
        }
开发者ID:rocketeerbkw,项目名称:DNA,代码行数:25,代码来源:IIsInitialise.cs


注:本文中的System.ServiceProcess.ServiceController.Start方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。