Normally you shouldn't have to tell a thread to close. It should be able to do its job then finish automatically.
I don't see a need to use Thread.Abort() at all.
Normally, what I would do is have the thread checking a variable on whether it should close e.g.
vb
Private Sub CodeInThread()
Dim keepRunning As Boolean = True
While keepRunning 'keeps thread running forever
'...
'Do all the fancy code that you have inside your thread
'...
'We lock, because that is one way of ensuring that no other thread
'is accessing the same variables at the same time
SyncLock accessLock
If endThread Then
keepRunning = False
End If
End SyncLock
End While 'loop forever until keepRunning = False
'keepRunning now equals false
'put cleanup code here
'when the sub finishes, the thread ends
End Sub
So the above code needs access to some variables. So in the same class the following variables should be defined
vb
Dim accessLock As New Object
Dim endThread As Boolean = False
Then if we want to stop the thread, we just set the stopThread value to True
vb
Public Sub StopThread()
SyncLock accessLock
endThread = True
End SyncLock
End Sub
This way, there will be no exception, and also your thread has more control over what it does when it finishes. E.g. if it needs to finish, it may want to do some clean up first.
This post has been edited by Nayana: 26 Feb, 2008 - 05:46 PM