Simple Thread stop

zinnqu 0 Tallied Votes 3K Views Share

This is a great example for a thread stop. Simple debugging trick for any looped process that needs the "e-brake" applied while making sure that the thread is working correctly. The idea is that you can manually stop a thread, to prevent that hanging sensation during any process. This works great with TCP and UDP protocol programs and ideal for all multi-threading programs.. feel free to implement as needed.

'Simulated loop that will continue for ever,
'Kinda like a persistant thread that will not stop
Private Sub BackgroundProcess()
    Dim i As Integer = 1

    Do While True
        ListBox1.Items.Add("Iterations: " + i)
        i += 1
    Loop
End Sub

'Thread created to handle the Background process
Dim t As Thread
t = New Thread(AddressOf Me.BackgroundProcess)
t.Start()

'add a button to the form to handle the "e-Brake" to the thread
Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
    t.Abort()
End Sub