Start stop Service from Form App c#

后端 未结 5 1936
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 19:11

How can I start and stop a windows service from a c# Form application?

相关标签:
5条回答
  • 2020-12-04 20:02

    First add a reference to the System.ServiceProcess assembly.

    To start:

    ServiceController service = new ServiceController("YourServiceName");
    service.Start();
    var timeout = new TimeSpan(0, 0, 5); // 5seconds
    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
    

    To stop:

    ServiceController service = new ServiceController("YourServiceName");
    service.Stop();
     var timeout = new TimeSpan(0, 0, 5); // 5seconds
    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
    

    Both examples show how to wait until the service has reached a new status (running, stopped...etc.). The timeout parameter in WaitForStatus is optional.

    0 讨论(0)
  • 2020-12-04 20:05

    Add a reference to System.ServiceProcess.dll. Then you can use the ServiceController class.

    // Check whether the Alerter service is started.
    ServiceController sc  = new ServiceController();
    sc.ServiceName = "Alerter";
    Console.WriteLine("The Alerter service status is currently set to {0}", 
                       sc.Status.ToString());
    
    if (sc.Status == ServiceControllerStatus.Stopped)
    {
       // Start the service if the current status is stopped.
       Console.WriteLine("Starting the Alerter service...");
       try 
       {
          // Start the service, and wait until its status is "Running".
          sc.Start();
          sc.WaitForStatus(ServiceControllerStatus.Running);
    
          // Display the current service status.
          Console.WriteLine("The Alerter service status is now set to {0}.", 
                             sc.Status.ToString());
       }
       catch (InvalidOperationException)
       {
          Console.WriteLine("Could not start the Alerter service.");
       }
    }
    
    0 讨论(0)
  • 2020-12-04 20:05

    there is a dirtier, but same same..
    just execute the shell command

    NET STOP "MYSERVICENAME"
    NET START "MYSERVICENAME"
    

    EDIT: Would like to expand on "dirtier":
    1. it is slower.
    2. this can result in some clever vulnerabilities
    3. it is a code that is not "understandable"
    4. it is highly not portable (surely if you use .NET Core)

    I am sure there are more wrong with it...
    but if you search for a small, inhouse tool... it'll do.
    (would like to say temporary, but temp stuff stick best!)

    0 讨论(0)
  • 2020-12-04 20:06

    You can do it like this, Details of Service Controller

    ServiceController sc = new ServiceController("your service name");
    if (sc.Status == ServiceControllerStatus.Stopped)
    {
      sc.Start();
    
    }
    

    Similarly you can stop using stop method

      sc.Stop();
    
    0 讨论(0)
  • 2020-12-04 20:16
    // Check whether the U-TEST RTC service is started.
            ServiceController sc = new ServiceController();
            sc.ServiceName = "U-TEST RTC";
            m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgStatusService"), sc.Status.ToString()), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log);
    
            if (sc.Status == ServiceControllerStatus.Stopped)
            {
                m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgStartService")), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log);
                try
                {
                    // Start the service, and wait until its status is "Running".
                    sc.Start();
                    var timeout = new TimeSpan(0, 0, 5); // 5seconds
                    sc.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgNowService"), sc.Status.ToString()), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log);
                }
                catch (InvalidOperationException)
                {
                    m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgExceptionStartService")), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log);
                }
            }
    
    0 讨论(0)
提交回复
热议问题