从表单应用程序启动停止服务c#

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从表单应用程序启动停止服务c#相关的知识,希望对你有一定的参考价值。

如何从c#Form应用程序启动和停止Windows服务?

答案

添加对System.ServiceProcess.dll的引用。然后你可以使用ServiceController类。

// 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.");
   }
}
另一答案

首先添加对System.ServiceProcess程序集的引用。

开始:

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

停止:

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

这两个示例都显示了如何等待服务达到新状态(运行,停止等等)。 WaitForStatus中的timeout参数是可选的。

另一答案

你可以这样做,Details of Service Controller

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

}

同样,您可以停止使用stop方法

  sc.Stop();
另一答案

有一个更脏,但同样相同.. 只需执行shell命令

NET STOP "MYSERVICENAME"
NET START "MYSERVICENAME"
另一答案
// 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);
            }
        }

以上是关于从表单应用程序启动停止服务c#的主要内容,如果未能解决你的问题,请参考以下文章

ActivityNotFoundException,启动活动时出错

如果应用程序被用户从应用程序管理器停止,想要重新启动服务。

如何在 C# mvc 中停止或启动 Windows 服务?

ForegroundService没有从片段开始?

C# dotAPNS 推送通知停止服务器上的工作,需要重新启动服务器才能使其正常工作

作为启动 RDP 程序运行时如何停止初始形式最大化?