Killing a thread

  • Thread starter Thread starter Bob Day
  • Start date Start date
B

Bob Day

Using VS 2003, VB, MSDE...

I am stoping a thread with the following code:
Try
Thread.CurrentThread.Abort()

Catch ex As Exception

MessageBox.Show(ex.ToString)

End Try

The MessagBox shows, but then after the End Try it traverses the call stack
all the way to the top (same thread) and the exception is caught again by a
Try/Catch that is around everthing that happens in this thread.

1) Why does the top level try catch get involved at all when it is caught
locally?

2) I don't really want to thow an exception, I just want to kill the thread.
It seems that setting it to nothing accomplishes the same thing. Is there
any disadvantage to doing it this way?

Thanks!
Bobg
 
Bob,
1) Why does the top level try catch get involved at all when it is caught
locally?

Thread.Abort() throws a ThreadAbortException. According to its
documentation

"ThreadAbortException is a special exception that can be caught, but
it will automatically be raised again at the end of the catch block."

You can't both terminate a thread and still execute code on it.

It seems that setting it to nothing accomplishes the same thing.

How did you come to that conclusion? Setting a reference to Nothing
doesn't terminate a thread.



Mattias
 
Well, then, is ther any way to kill a thread other that .Abort? Without
throwing an exception?

Thanks
Bob Day
 
Yes, set a flag in the thread - if your thread executes a loop that is, then
"while not bDone" - other thread sets "bDone" to true when it wants the
thread to stop. Of course, you will have no way of knowing exactly when the
thread has stopped (it might execute another iteration).
 
Hi Bob,

I agree with Robin's suggestion, if you still have any question on this
issue, please feel free to post here, and I will work on your with it.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top