From: yxq on
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


From: Patrice on
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(a)163.net> a �crit dans le message de groupe de discussion :
eebnBEMpKHA.1548(a)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
>

From: Armin Zingler on
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
From: yxq on
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