End thread by ID

I am trying to handle some threads. I saw that you can get a unique identifier from the stream or set the name of the stream. But how can I terminate / interrupt a stream by its identifier? I do not believe that .NET has its own abilities to do it right? Maybe Win32-API?

+4
source share
1 answer

You can get a list of all the threads of the operating system (represented by the ProcessThread class ) of the current process using Process.GetCurrentProcess().Threads , but you cannot interrupt them this way.

On the other hand, you can interrupt managed threads (represented by the Thread class ), but there seems to be a way to list all threads (with the possible exception of using the unmanaged debugging API).

What you can do to save all the threads you create in the list, and when you want to interrupt one of them, use the list to find it.

BUT thread interruption is very bad, and you should not do this, if at all possible. There is probably a better way, what exactly are you trying to do?

+2
source

All Articles