I have an intermittent problem where I get the unhandled exception 'thread was being aborted'.
I've tried putting the abort statement inside a TRY - CATCH, but that hasn't helped.
So, I did some googling, and found the following code which suggests using a boolean variable to let the thread finish, but it doesn't work - I can click on the stop button until I'm blue in the face....
Any idea why this isn't working?
Here's the code for the module....
Module Module1
Public accesslock As Object
Public endThread As Boolean = False
Public Sub CodeInThread()
Dim KeepRunning As Boolean = True
Dim frmPleaseWait As New Form
Dim PleaseWaitPicture As New PictureBox
PleaseWaitPicture.Height = 130
PleaseWaitPicture.Width = 130
PleaseWaitPicture.Image = My.Resources.loading51
frmPleaseWait.FormBorderStyle = FormBorderStyle.None
frmPleaseWait.Height = 130
frmPleaseWait.Width = 130
frmPleaseWait.BackColor = Color.Aquamarine
frmPleaseWait.StartPosition = FormStartPosition.CenterScreen
frmPleaseWait.Controls.Add(PleaseWaitPicture)
frmPleaseWait.ShowDialog()
frmPleaseWait.BringToFront()
While KeepRunning
If endThread Then
KeepRunning = False
End If
End While
frmPleaseWait.Hide()
End Sub
End Module
...and here's the code for the form....
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim t As New Threading.Thread(AddressOf CodeInThread)
t.Start()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
endThread = True
End Sub
.... note that the original code used the synclock statements but that throws a 'value cannot be null' exception when the stop button is clicked.