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
精彩评论