How to stop a service and wait until service stopped?

  • Follow


I want to stop a service and wait until it has been stopped, then do 
something, but the code below did not work?
////////////////////////////////////////////////////////////////////
       Dim a As New ServiceController("cryptsvc")
        a.Stop()
        Do Until a.Status = ServiceControllerStatus.Stopped
            Application.DoEvents()
        Loop
        MessageBox.Show("Stopped!")
//////////////////////////////////////////////////////////////////////
Could anyone tell a method to detect the service has been stopped?
Thank you 


0
Reply yxq 2/3/2010 11:06:33 AM

yxq schrieb:
> I want to stop a service and wait until it has been stopped, then do 
> something, but the code below did not work?
> ////////////////////////////////////////////////////////////////////
>        Dim a As New ServiceController("cryptsvc")
>         a.Stop()
>         Do Until a.Status = ServiceControllerStatus.Stopped
>             Application.DoEvents()
>         Loop
>         MessageBox.Show("Stopped!")
> //////////////////////////////////////////////////////////////////////
> Could anyone tell a method to detect the service has been stopped?
> Thank you 


Replace the loop with

    a.WaitForStatus(ServiceControllerStatus.Stopped)


If you used a loop, you would have to call a.Refresh. You also would have
a CPU intensive loop, and Application.Doevents is not required. Instead,
remove the cause for DoEvents being necessary: The loop. Move it to a
different thread. Anyway, WaitForStatus makes this all needless.

-- 
Armin
0
Reply Armin 2/3/2010 11:30:03 AM


Hello,

Just check the doc for the available members and you'll find :

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.waitforstatus(VS.80).aspx 
that should fit your needs (a mthod that just waits for a service to be in a 
particular state with an optional timeout).

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.refresh(VS.80).aspx 
that likely explains why your current approach doesn't work (status is 
likely cached and needs to be refreshed)...

--
Patrice


"yxq" <gayxq@163.net> a �crit dans le message de groupe de discussion : 
eebnBEMpKHA.1548@TK2MSFTNGP06.phx.gbl...
> I want to stop a service and wait until it has been stopped, then do 
> something, but the code below did not work?
> ////////////////////////////////////////////////////////////////////
>       Dim a As New ServiceController("cryptsvc")
>        a.Stop()
>        Do Until a.Status = ServiceControllerStatus.Stopped
>            Application.DoEvents()
>        Loop
>        MessageBox.Show("Stopped!")
> //////////////////////////////////////////////////////////////////////
> Could anyone tell a method to detect the service has been stopped?
> Thank you
> 

0
Reply Patrice 2/3/2010 11:59:53 AM

Thank you, i will try that.

>> I want to stop a service and wait until it has been stopped, then do
>> something, but the code below did not work?
>> ////////////////////////////////////////////////////////////////////
>>        Dim a As New ServiceController("cryptsvc")
>>         a.Stop()
>>         Do Until a.Status = ServiceControllerStatus.Stopped
>>             Application.DoEvents()
>>         Loop
>>         MessageBox.Show("Stopped!")
>> //////////////////////////////////////////////////////////////////////
>> Could anyone tell a method to detect the service has been stopped?
>> Thank you
>
>
> Replace the loop with
>
>    a.WaitForStatus(ServiceControllerStatus.Stopped)
>
>
> If you used a loop, you would have to call a.Refresh. You also would have
> a CPU intensive loop, and Application.Doevents is not required. Instead,
> remove the cause for DoEvents being necessary: The loop. Move it to a
> different thread. Anyway, WaitForStatus makes this all needless.
>
> -- 
> Armin 


0
Reply yxq 2/3/2010 1:36:57 PM

3 Replies
1160 Views

(page loaded in 0.075 seconds)


Reply: