|
-
May 27th, 2008, 05:18 AM
#1
Thread Starter
Lively Member
die thread die!
Hello,
i have a problem with thread.abort. i am using it instead of autoreset event because I have minimal cleanup and when it works it is a lot quicker. I have included the core bits that I am using. btnStartExecution creates the background thread and btnStopExecution is supposed to stop it. what i am trying to do is force a ThreadAbortException and then allow join to do the rest. Unfortunately I can't seem to get Thread.Abort to throw an exception and this is what I need help with. The problem is in the code below in abortThread method. I am cleaning up manually using a custom event run.Stopped, but still need the thread to die before carrying on. Any help appreciated.
Code:
Private Delegate Sub _StopExecution_delegate()
Private Delegate Sub _abortThread_delegate()
Private Sub btnStartExecution_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartExecution.Click
'//m_thread is global thread
m_Thread = New Thread(AddressOf StartExecution)
With m_Thread
.IsBackground = True
.Start()
End With
End Sub
Private Sub btnStopExecution_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopExecution.Click
frmTreeViewInstance.Invoke(New _StopExecution_delegate(AddressOf StopExecution))
End Sub
Public Sub StopExecution()
Dim strMessage As String = "Halting execution..."
report.Write("StopExecution", strMessage, report.Warning, 1) '//custom reporting function
run.RunStatus = run.Stopped '//handles minimal cleanup required.
Dim del As _abortThread_delegate = New _abortThread_delegate(AddressOf abortThread)
del.Invoke()
End Sub
Private Sub abortThread()
Try
If m_Thread.IsAlive Then
m_Thread.Abort()
End If
Catch
'//no ThreadAbortException ever caught?!?
Finally
Do Until m_Thread.Join(500)
Application.DoEvents()
Loop
Dim del As RunStateDelegate = New RunStateDelegate(AddressOf SetRunStateStopped)
del.Invoke()
End Try
End Sub
-
May 27th, 2008, 05:31 AM
#2
Re: die thread die!
Hi,
Here's a link about the Thread Abort method and the ThreadAbortException:
http://msdn.microsoft.com/en-us/libr...64(VS.71).aspx
It could help you,
sparrow1
-
May 27th, 2008, 07:31 AM
#3
Re: die thread die!
Aborting threads in general can be bad. While it's more work, I find that putting loop-breakers in my thread routine works best to kill them gracefully with minimal problems. Then, all you need is a Thread.Join()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|