Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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....

VB
Module Module1

    Public accesslock As Object
    Public endThread As Boolean = False

    Public Sub CodeInThread()
        Dim KeepRunning As Boolean = True

        'define and show the form.
        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
            'keeps thread running forever.

            'SyncLock accesslock
            If endThread Then
                KeepRunning = False
            End If
            'End SyncLock
        End While

        frmPleaseWait.Hide()

    End Sub

End Module


...and here's the code for the form....



VB
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

    'SyncLock accesslock
    endThread = True
    'End SyncLock

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.
Posted
Comments
AnkitGoel.com 10-Dec-12 8:55am    
u r right it cannot be helped. the only solution i know is- you have to keep the boolean variable false and keep that statement out of try-catch block.

1 solution

There's a few problems with this.

First, you don't need the SyncLock around the variables controling if the thread runs or not. Since you only have one writer and one reader of the variable, you'll never get into a situation where a SyncLock would do you any good.

Second, you only need one variable to control the thread terminating, not 2. A simple boolean property called Running (or similar) will do the trick.

Lastly, and this is the BIG one, you can NOT create a form on a background thread and show it. All controls and forms MUST be created on the UI (startup) thread and then can only be touched by the UI thread. You cannot manipulate controls from anything other than the UI thread.
 
Share this answer
 
Comments
Member 8646699 11-Dec-12 9:38am    
OK - I've changed the code as follows - module is now...

Module Module1

Public endThread As Boolean = False
Public frmPleaseWait As New Form
Public PleaseWaitPicture As New PictureBox

Public Sub CodeInThread()

frmPleaseWait.ShowDialog()
frmPleaseWait.BringToFront()

While Not endThread
'keep thread running forever.
End While

frmPleaseWait.Hide()

End Sub

End Module


and the form is as follows...

Public Class Form1


Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

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)

endThread = False

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
End Class


...but the thread still won't stop.

In case it helps, this was to display a 'please wait' animation, I was trying to use a thread as the process can take different lengths of time, so I needed a way to start it, carry on processes in the 'main' thread, and then stop the please wait.
Dave Kreskowiak 11-Dec-12 10:27am    
Again, you cannot touch a control from a background thread. That includes .ShowDialog, .BringToFront and .Hide.

The only thing you should be running on the thread is the long-running code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900