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




Reply With Quote