0

Possible Duplicate:
Getting list of currently active managed threads in .NET?

In a c# method I start several threads simultaneously with the following construct:

new Thread(() =>
{
    Application.Run(...);
}).Start();

How can I get the list of all active threads afterwards and kill them all excluding the main thread?

Community
  • 1
  • 1
selmar
  • 181
  • 3
  • 14

1 Answers1

0

If you really want to kill the thread, capture the return value from new Thread() and call myThread.Abort()

Thread myThread = new Thread(() =>
{
    Application.Run(...);
}).Start();

// ...

myThread.Abort();

http://msdn.microsoft.com/en-us/library/system.threading.thread.abort.aspx

However it is preferable to end the thread cooperatively.

If you do not want to capture the thread and track it yourself, you will probably have to use the Profiling/Debugging API to enumerate threads in your process

How can I enumerate all managed threads in C#?

To learn more about cooperative threading, and about threading in general, I suggest the excellent article

http://www.albahari.com/threading/

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • This helped a lot, thanks. I wanted to, but couldn't give any points as my reputation score is below the required limit 15. – selmar Jan 17 '13 at 16:28