Proper way to terminate thread

Peter Volz 1,295 Reputation points
2023-10-19T13:01:10.29+00:00

Hello

I use Thread to run a heavy code in non-UI mode:

Define it to be accessible in the whole form:

Private MyThread As Thread

Under Cancel Button:

If MyThread IsNot Nothing Then MyThread.Abort()

And starting thread:

MyThread = New Thread(New ThreadStart(AddressOf ThreadRoute))

MyThread.Start()

The problem?

5 System.Threading.ThreadAbortExceptions and this error:

An exception of type 'System.Threading.ThreadAbortException' occurred in blah.exe but was not handled in user code

Where and how should I handle cancellation?

Thanks :)

Developer technologies VB
{count} votes

4 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2023-10-19T13:46:53.85+00:00

    Hi,@Peter Volz. Welcome to Microsoft Q&A Forum.

    To handle cancellation more effectively, you could use the CancellationToken and CancellationTokenSource classes, which provide a cooperative cancellation mechanism.

    Here's an example of how you can use the CancellationTokenSource and CancellationToken for cooperative cancellation:

    Imports System.Threading
    
    Class MainWindow
    
        Private cancellationTokenSource As CancellationTokenSource
    
        Private Sub CancelButton_Click(sender As Object, e As EventArgs)
            If cancellationTokenSource IsNot Nothing Then
                cancellationTokenSource.Cancel()
            End If
        End Sub
    
        Private Sub ThreadRoute()
            Dim token As CancellationToken = cancellationTokenSource.Token
    
            ' For example:
            While True
                If token.IsCancellationRequested Then
                    ' Clean up or save the current state before exiting
                    Exit While
                End If
                ' Perform heavy work here
            End While
        End Sub
    
        Private Sub StartThread()
            cancellationTokenSource = New CancellationTokenSource()
    
            Dim thread As New Thread(AddressOf ThreadRoute)
            thread.Start()
        End Sub
    
    
    End Class
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. RLWA32 49,536 Reputation points
    2023-10-19T13:11:51.8866667+00:00
    0 comments No comments

  3. Dewayne Basnett 1,381 Reputation points
    2023-10-19T13:19:27.7466667+00:00

    Thread abort is typically not the way to do this, though there are exceptions.

    Here is the proper way.

    https://learn.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads

    0 comments No comments

  4. KOZ6.0 6,655 Reputation points
    2023-10-19T13:45:37.7866667+00:00

    Some older programs may have dangerous usage like this. When aborting, also check the IsAlive property, execute the Abort method, execute the Join method, and wait until it finishes.

    If MyThread IsNot Nothing Then
        If MyThread.IsAlive Then
            MyThread.Abort()
        End If
        MyThread.Join()
    End If
    

    Note that this is not a good implementation.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.