By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,163 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,163 IT Pros & Developers. It's quick & easy.

Suspend/resume Thread problems

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

"juky" <ju*******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Nov 21 '05 #2

P: n/a
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.StopPend ing)

' TODO: Decide if this first RaiseEvent really needed? RaiseEvent StatusEventRaise
ServiceControllerStatus.StopPending)
svc.WaitForStatus(ServiceControllerStatus.Stopped) RaiseEvent
StatusEventRaise(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.Start() 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
"juky" <ju*******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... 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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.