E
eBob.com
I've written some simple multi-threading code so that I can play with
Aborting and restarting threads. The Abort seems to happen according to
MsgBoxes in the code, but the thread then seems to switch back to a Running
state and is therefore not re-started.
The code is below. In more detail ... The code in StartMainThread (which is
not the UI thread) starts two threads, Thread2 and Thread3. MsgBoxes
indicate that they start. All they do is wait for 30 seconds. But if they
Catch an Exception they wake up their parent, i.e. the code in
StartMainThread, which will restart the thread if its state is Aborted,
AbortRequested, or Stopped. Code not shown below issues the Thread.Abort
for Thread2 and the MsgBox in StartSubThread shows its state as
AbortRequested. Then the code in StartMainThread does wake up, but
according to the MsgBox there is seeing the thread's state as Running. How
can that be? What am I missing?
Thanks, Bob
Sub StartMainThread()
Dim MeThread As Threading.Thread
MeThread = Threading.Thread.CurrentThread
MsgBox(MeThread.Name & " started, TID = " &
MeThread.ManagedThreadId.ToString)
MainWaitHandle = New AutoResetEvent(False)
Thread2 = New Threading.Thread(AddressOf StartSubThread)
Thread2.Name = "Thread2"
Thread2.Start()
Thread3 = New Threading.Thread(AddressOf StartSubThread)
Thread3.Name = "Thread3"
Thread3.Start()
Do While True
MainWaitHandle.WaitOne() ' wait for either thread to siginal
MsgBox("Main woke up")
MsgBox("thread2.threadstate = " & MeThread.ThreadState.ToString)
If (Thread2.ThreadState Or (ThreadState.Aborted Or _
ThreadState.AbortRequested Or
ThreadState.Stopped)) = 1 Then
MsgBox("Thread2 kaput, restarting it.")
Thread2 = New Threading.Thread(AddressOf StartSubThread)
Thread2.Name = "Thread2"
Thread2.Start()
End If
If (Thread3.ThreadState Or (ThreadState.Aborted Or _
ThreadState.AbortRequested Or
ThreadState.Stopped)) = 1 Then
MsgBox("Thread3 kaput, restarting it.")
Thread3 = New Threading.Thread(AddressOf StartSubThread)
Thread3.Name = "Thread3"
Thread3.Start()
End If
Loop
End Sub
Sub StartSubThread()
Dim MeThread As Threading.Thread
MeThread = Threading.Thread.CurrentThread
MsgBox(MeThread.Name & " started, TID = " &
MeThread.ManagedThreadId.ToString)
Try
Thread.Sleep(30000)
Catch ex As Exception
MsgBox("Exception Caught: " & ex.ToString)
MsgBox(MeThread.ThreadState.ToString)
MainWaitHandle.Set()
End Try
MsgBox(MeThread.Name & " ending, TID = " &
MeThread.ManagedThreadId.ToString)
End Sub
Aborting and restarting threads. The Abort seems to happen according to
MsgBoxes in the code, but the thread then seems to switch back to a Running
state and is therefore not re-started.
The code is below. In more detail ... The code in StartMainThread (which is
not the UI thread) starts two threads, Thread2 and Thread3. MsgBoxes
indicate that they start. All they do is wait for 30 seconds. But if they
Catch an Exception they wake up their parent, i.e. the code in
StartMainThread, which will restart the thread if its state is Aborted,
AbortRequested, or Stopped. Code not shown below issues the Thread.Abort
for Thread2 and the MsgBox in StartSubThread shows its state as
AbortRequested. Then the code in StartMainThread does wake up, but
according to the MsgBox there is seeing the thread's state as Running. How
can that be? What am I missing?
Thanks, Bob
Sub StartMainThread()
Dim MeThread As Threading.Thread
MeThread = Threading.Thread.CurrentThread
MsgBox(MeThread.Name & " started, TID = " &
MeThread.ManagedThreadId.ToString)
MainWaitHandle = New AutoResetEvent(False)
Thread2 = New Threading.Thread(AddressOf StartSubThread)
Thread2.Name = "Thread2"
Thread2.Start()
Thread3 = New Threading.Thread(AddressOf StartSubThread)
Thread3.Name = "Thread3"
Thread3.Start()
Do While True
MainWaitHandle.WaitOne() ' wait for either thread to siginal
MsgBox("Main woke up")
MsgBox("thread2.threadstate = " & MeThread.ThreadState.ToString)
If (Thread2.ThreadState Or (ThreadState.Aborted Or _
ThreadState.AbortRequested Or
ThreadState.Stopped)) = 1 Then
MsgBox("Thread2 kaput, restarting it.")
Thread2 = New Threading.Thread(AddressOf StartSubThread)
Thread2.Name = "Thread2"
Thread2.Start()
End If
If (Thread3.ThreadState Or (ThreadState.Aborted Or _
ThreadState.AbortRequested Or
ThreadState.Stopped)) = 1 Then
MsgBox("Thread3 kaput, restarting it.")
Thread3 = New Threading.Thread(AddressOf StartSubThread)
Thread3.Name = "Thread3"
Thread3.Start()
End If
Loop
End Sub
Sub StartSubThread()
Dim MeThread As Threading.Thread
MeThread = Threading.Thread.CurrentThread
MsgBox(MeThread.Name & " started, TID = " &
MeThread.ManagedThreadId.ToString)
Try
Thread.Sleep(30000)
Catch ex As Exception
MsgBox("Exception Caught: " & ex.ToString)
MsgBox(MeThread.ThreadState.ToString)
MainWaitHandle.Set()
End Try
MsgBox(MeThread.Name & " ending, TID = " &
MeThread.ManagedThreadId.ToString)
End Sub