473,287 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Suspend/resume Thread problems

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
2 4257
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Keith Langer | last post by:
I have an application which consists of a main work thread and multiple threads which each maintain a TCP socket. When a configuration change occurs, all activity on the socket threads must be...
8
by: Wim | last post by:
My GUI application starts a process (a console program) when the user hits Play. I would like to add an option to pause that process. The code I've added to detect if the user hit pause/unpause...
2
by: Sakharam Phapale | last post by:
Hi All, In following example, while playing file, if thread goes in WaitSleepJoin state, due to Thread.Sleep method. Now I want to suspend thread by clicking on cmdSuspend button. I have...
2
by: Alan T | last post by:
As the Suspend method is deprecated, what method can I use? What I want to do is when the user press the button "Cancel", it will prompt the user to confirm it he want to stop the process, I need...
6
by: Buddy Home | last post by:
Hello, I want to understand whats the best way to write code to replace Thread.Suspend, Thread.Resume and Thread.Abort. I have lots of code calling these existing methods and want to minimize...
4
by: wanwan | last post by:
I'm using Microsoft Visual Studio 2005 in multithreading, I want to be able to pause and resume a thread. I see that suspend and resume are deprecated. So what can I use instead.
4
by: fAnSKyer/C# newbie | last post by:
I am using winmm.dll and I found that I can't just suspend it and resume it? What should I do? Any better idea? Should I use thread? and thread.suspend will work? Thanks
3
by: =?Utf-8?B?TWFyayBDaGFubmluZw==?= | last post by:
I have a code which registers all threads with a thread dump class. At intervals this thread dump class will dump the stack trace of all threads. As calling StackTrace(threadtoDump) from a...
1
by: Mike Fellows | last post by:
Having not used threads for such a long time in vb.net I have found myself in need of them I have a thread that runs and retirieves live data from a SQL database, this is running fine and has...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.