Suspend/resume Thread problems

J

juky

Hi all,

I have a loop in the thread checking for a particular service status,
whenever the status changes to "stopped" a RaiseEvent is generated by
thread and another function runs. At the same time the thread is
suspended. When I try to resume it from the another function I have
some problems.

Thank you.
Juky

Here the code:

Public Event StatusEventRaise(ByVal EventGenerated As Integer)

Dim StatusService As Threading.Thread
Private svc As ServiceController
.......

Public Sub New()
.....
AddHandler StatusEventRaise, AddressOf StatusEventRaiseHandler
StatusService = New Threading.Thread(AddressOf LoopCheck)
StatusService.Start()
....
end Sub

Private Sub LoopCheck()
While True
svc.Refresh()
Select Case svc.Status
Case ServiceControllerStatus.StopPending
RaiseEvent StatusEventRaise
ServiceControllerStatus.StopPending)
StatusService.Suspend()
Case ServiceControllerStatus.Stopped
RaiseEvent
StatusEventRaise(ServiceControllerStatus.Stopped)
Me.StatusService.Suspend()
End Select
End While
End Sub


Private Sub StatusEventRaiseHandler(ByVal EventGenerated As Integer)
Static nr As Integer = 0

Select Case EventGenerated
Case ServiceControllerStatus.Stopped
svc.Start()
StatusService.Resume()
Case ServiceControllerStatus.StopPending
While svc.Status <> ServiceControllerStatus.Stopped
svc.Refresh()
End While
svc.Start()
while svc.Status <> ServiceControllerStatus.Running
svc.Refresh()
End While
StatusService.Resume()
End Select
End Sub
 
N

Nick

Sorry, I haven't really looked through your code but, I would check that
each class that you are using within your functions is thread safe.
For instance MSDN says that only static/shared instances your
ServiceController class are guaranteed to be threadsafe.
 
J

Jay B. Harlow [MVP - Outlook]

Juky,
In addition to the other comments.
When I try to resume it from the another function I have
some problems.
What specifically is the problem? Remember if you suspend a thread, another
thread needs to resume that thread. From your code you are suspending the
thread on the same thread that is handling the event, then on the exact same
thread you attempt to resume yourself. You put yourself to sleep, someone
else will need to wake you!

Also I would suggest you investigate ServiceController.WaitForStatus. Using
WaitForStatus may actually be what you want rather then Suspend & Resume!
Dim StatusService As Threading.Thread
Private svc As ServiceController
This seems like an Odd Ball solution. You have a Long verbose name for the
Thread, yet a short obscure name for the ServiceController. In 6 months when
you visit this code, I'm sure it will be far easier to understand if you
used the same naming convention for both! Especially since "StatusService"
sounds like the name for a ServiceController or ServiceBase object, not a
Thread object!


Something like:
Private Sub LoopCheck()
While True

' TODO: Decide if this first wait really needed?
svc.WaitForStatus(ServiceControllerStatus.StopPending)

' TODO: Decide if this first RaiseEvent really needed?
RaiseEvent StatusEventRaise
ServiceControllerStatus.StopPending)
svc.WaitForStatus(ServiceControllerStatus.Stopped)

End While
End Sub

Private Sub StatusEventRaiseHandler(ByVal EventGenerated As Integer)
Static nr As Integer = 0

Select Case EventGenerated
Case ServiceControllerStatus.Stopped
svc.Start()
Case ServiceControllerStatus.StopPending svc.WaitForStatus(ServiceControllerStatus.Stopped)
svc.WaitForStatus(ServiceControllerStatus.Running)
End Select
End Sub

The "problem" with WaitForStatus is that it can only wait for a single
Status, so your LoopCheck, as you wrote it, does not make as much sense, I
would consider using the WaitForStatus overload that expects a TimeSpan &
have it time out, however this causes an exception... Having LoopCheck check
for a single status may make more sense...

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top