开发者

How to verify if a windows service is stopped or running and wait until it is in this state?

开发者 https://www.devze.com 2023-01-16 22:44 出处:网络
How to ver开发者_StackOverflow社区ify if a windows service is stopped or running and wait until it is in this state?

How to ver开发者_StackOverflow社区ify if a windows service is stopped or running and wait until it is in this state?

Dim s As New ServiceController("Aservice")
    s.Refresh()
    If s.Status = ServiceControllerStatus.Running Then
        s.Stop()
    End If
    s.Refresh()

The problem is that I want to wait in this function until the service is in that state... How can I do this? tnx!


You can add a little While loop:

Dim MaxWait = 10
While Not s.Status = ServiceControllerStatus.Stopped
  System.Threading.Thread.Sleep(100)
  MaxWait = MaxWait - 1
  If MaxWait < 1 Then Break
End While

But you have to think about "What if it doesn't stop ?"


Dim s As New ServiceController("Aservice")

While s.Status <> ServiceControllerStatus.WhatEverState
    Thread.Sleep(1000)
    s.Refresh()
End While
0

精彩评论

暂无评论...
验证码 换一张
取 消