How to kill a particular thread

  • Thread starter Thread starter RickDee
  • Start date Start date
R

RickDee

Understand that when I start a thread, a number will be generated and is
able to get from GetHashCode method. But I would like to use this number
when I want to kill certain thread, anybody know how ??

Thanks
Regards
 
If you can keep track of the hashcode why not just keep track of the thread
object itself?

Currently there is no managed API that will enumerate the managed thread
objects, so if all you have is the hashcode there is no documented way of
converting that back to a thread object. When you create the thread you
should save the thread reference and use that later when you want to stop
the thread.
 
Dave,

Hi. First of all thanks for your help.

So what are you saying is, if for example, I have the following statement :

th1 = new System.Threading.Thread(new
System.Threading.ThreadStart(abc.Run));
th2= new System.Threading.Thread(new System.Threading.ThreadStart(abc.Run));
th3 = new System.Threading.Thread(new
System.Threading.ThreadStart(abc.Run));

then I have to use :

th1.Abort()
th2.Abort()
th3.Abort()

right ?

My purpose is actually have a button on the Winform and every time the
button is clicked, it will call :

th1 = new System.Threading.Thread(new
System.Threading.ThreadStart(abc.Run));

And I will then have another button on the winform that whe it is click,
user can select a particular thread to kill.

Thanks
Regards
 
That's basically the idea.

FWIW, there are no guarantees that the thread will be aborted. This can
happen if it makes a call to unmanaged code - the thread wont be aborted
until it returns to managed code. Another is that the thread must be
running - if it is suspended it wont be aborted until it is resumed. Another
is that the thread can catch the abort and reset it. And I am not positive
about this, but I believe the thread must be in an alertable wait state for
the abort exception to get delivered, which means it must be blocked on a
synchronization object.
 
Chirs,

Hi. :)

I think from the reply that you wrote, you definitely have the sample that I
need. I am interested in knowing how you are able to store them to a hash
table while creating the thread using GUID, and then able to kill them
individually.

Do you think you are able to shared one of the most simple example that you
have ? Appreciate that.

Thanks
Regards
 
Absolutely. I'll post some sample a little bit later when I dig back
into code and can take a moment to copy/paste. I'm just at the
computer now for a few minutes to check messages and such before
heading out for the evening.

Back to you soon.
 
Back
Top