Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
 
        '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....
 

 
    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 10-Dec-12 3:50am
Comments
AnkitGoel.com at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Member 8646699 at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 335
1 OriginalGriff 250
2 DamithSL 170
3 Kornfeld Eliyahu Peter 130
4 Peter Leow 95
0 OriginalGriff 7,315
1 DamithSL 5,199
2 Sergey Alexandrovich Kryukov 4,917
3 Maciej Los 4,866
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100