代码之家  ›  专栏  ›  技术社区  ›  Mark Pim

确保从Visual Basic.Net启动windows服务

  •  1
  • Mark Pim  · 技术社区  · 15 年前

    我在Windows服务中托管了一个WCF服务(使用讨论过的技术) here

    我可以写一些代码来确保Windows服务启动了吗?如果没有启动,我可以启动它吗?

    编辑: 当然,我可以确保服务启动设置为自动,但它不需要一直运行,即使这样,前端应用程序仍然需要确保服务正在运行,如果它没有运行,就启动它。

    2 回复  |  直到 15 年前
        1
  •  2
  •   Ash    15 年前

    你可以用 ServiceController

    ' Toggle the Telnet service - 
    ' If it is started (running, paused, etc), stop the service.
    ' If it is stopped, start the service.
    Dim sc As New ServiceController("Telnet")
    Console.WriteLine("The Telnet service status is currently set to {0}", sc.Status)
    
    If sc.Status.Equals(ServiceControllerStatus.Stopped) Or sc.Status.Equals(ServiceControllerStatus.StopPending) Then
       ' Start the service if the current status is stopped.
       Console.WriteLine("Starting the Telnet service...")
       sc.Start()
    Else
       ' Stop the service if its status is not set to "Stopped".
       Console.WriteLine("Stopping the Telnet service...")
       sc.Stop()
    End If
    
    ' Refresh and display the current service status.
    sc.Refresh()
    Console.WriteLine("The Telnet service status is now set to {0}.", sc.Status)
    
        2
  •  2
  •   Luhmann    15 年前

    你可以这样做

        Dim controller As New ServiceController("ServiceNameHere")
        If controller.Status = ServiceControllerStatus.Stopped Then
            controller.Start()
        End If