Well, the first thing you need is already doing, which is to save the ManagedThreadId .
For the user to abort, you also need to save the list to show only his, but that's not what the question is about.
The first step is to import the kernel32.dll DLL and use it to open and terminate the Thread with ManagedThreadId . It would look like this:
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern bool TerminateThread(IntPtr hThread, uint dwExitCode);
After this, just terminate the selected Thread, like this:
IntPtr ptrThread = OpenThread(1, false, (uint)id);
TerminateThread(ptrThread, 1);
See a complete example in the code below:
class Program
{
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern bool TerminateThread(IntPtr hThread, uint dwExitCode);
static void Main(string[] args)
{
Console.Out.WriteLine("Forking off threads...");
for (int i = 0; i < 5; i++)
{
Thread t = new Thread(new ThreadStart(RunInfinite));
Console.Out.WriteLine("Thread " + t.Name + "(ManagedThreadId: " + t.ManagedThreadId + ") created!");
}
ProcessThreadCollection processThreads = Process.GetCurrentProcess().Threads;
Console.Out.WriteLine("=> Total threads: " + processThreads.Count);
foreach (ProcessThread pt in processThreads)
{
int timerSeconds = 5;
while (timerSeconds-- > 0)
{
Console.Out.Write("\r Seconds before thread " + pt.Id + " dies: " + timerSeconds);
System.Threading.Thread.Sleep(1000);
}
IntPtr ptrThread = OpenThread(1, false, (uint)pt.Id);
if (AppDomain.GetCurrentThreadId() != pt.Id)
{
try
{
TerminateThread(ptrThread, 1);
Console.Out.Write(". Thread killed.\n");
}
catch (Exception e)
{
Console.Out.WriteLine(e.ToString());
}
}
else
Console.Out.Write(". Not killing... It's the current thread!\n");
}
Console.Out.WriteLine("=> Total threads now: " + Process.GetCurrentProcess().Threads.Count);
Console.ReadLine();
}
public static void RunInfinite()
{
while (true)
{
System.Threading.Thread.Sleep(10000);
}
}
}
Another way would be to this answer , where you would look up Threads and check if the id is the same, but this way I would not advise you much, because of what you said, you will have many Threads in your system.
public void KillThread(int index)
{
string id = string.Format("MyThread{0}",index);
foreach (Thread thread in _threads)
{
if (thread.Name == id)
thread.Abort();
}
}
References: