views:

25

answers:

2

How to verify 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!

+1  A: 
Dim s As New ServiceController("Aservice")

While s.Status <> ServiceControllerStatus.WhatEverState
    Thread.Sleep(1000)
    s.Refresh()
End While
Andrey
+1  A: 

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 ?"

Henk Holterman
Yes, and if a put a 99 times counter... it is posibile to raech that count ...
Adrian