469,352 Members | 1,661 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

How to kill a particular thread


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
Nov 15 '05 #1
6 42529
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.

"RickDee" <ch***********@agilent.com> wrote in message
news:u0****************@TK2MSFTNGP12.phx.gbl...

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

Nov 15 '05 #2
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
"Dave" <kd******@wi.rr.com> wrote in message
news:u0**************@TK2MSFTNGP12.phx.gbl...
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.

"RickDee" <ch***********@agilent.com> wrote in message
news:u0****************@TK2MSFTNGP12.phx.gbl...

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


Nov 15 '05 #3
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.
"RickDee" <ch***********@agilent.com> wrote in message
news:uZ**************@TK2MSFTNGP09.phx.gbl...
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
"Dave" <kd******@wi.rr.com> wrote in message
news:u0**************@TK2MSFTNGP12.phx.gbl...
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.

"RickDee" <ch***********@agilent.com> wrote in message
news:u0****************@TK2MSFTNGP12.phx.gbl...

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



Nov 15 '05 #4
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
"Chris Hornberger" <ch***@chornbe.com> wrote in message
news:53**************************@posting.google.c om...
Yep, that's about it. What I do... and I use self-managed threads VERY
sparingly... is add them to a hash table using a GUID or something
similar as the key. Then, when I want to kill them 'til they're dead
or just get status on them, I spin thru' the hash table and take care
of business. If I've aborted a thread, or it has completed (.IsAlive
== false), then I drop them from the hash if I'm happy they'll go away
or I've .Join()'d them without error.

"RickDee" <ch***********@agilent.com> wrote in message

news:<uZ**************@TK2MSFTNGP09.phx.gbl>...
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 ?

Nov 15 '05 #5
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.
"RickDee" <ch***********@agilent.com> wrote in message news:<O3*************@TK2MSFTNGP09.phx.gbl>...
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

Nov 15 '05 #6
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.
"RickDee" <ch***********@agilent.com> wrote in message news:<O3*************@TK2MSFTNGP09.phx.gbl>...
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

Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Jerry Sievers | last post: by
9 posts views Thread by Brett | last post: by
2 posts views Thread by Christopher Carnahan | last post: by
1 post views Thread by AE_Cory | last post: by
18 posts views Thread by =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post: by
20 posts views Thread by =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post: by
1 post views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.