Search code examples
c#.netmultithreadingthread-abort

Aborting a thread via its name


I was wondering if it's possible to Abort (the dirty way) a thread using the threads name? here's some example code:

public void blah() {
   int TableID = 4; //set by the using at runtime

   Thread myThread = new Thread(() => beginUser(picture));
   myThread.Name = Convert.ToString(TableID);
   myThread.Start();
}

So now I have created a thread. Later in the program the user may end a thread, there is where my question comes in. How can I end a thread via it's name? or perhaps another way to end it? I don't want to use backround worker.

example: myThead[4].Abort();

thanks


Solution

  • Why not simply use a dictionary to store threadname to thread mapping and just kill from wherever you want.

    Dictionary<string, Thread> threadDictionary = new Dictionary<string, Thread>();
    Thread myThread = new Thread(() => beginUser(picture));
    myThread.Name = Convert.ToString(TableID);
    myThread.Start();
    threadDictionary.Add("threadOne", myThread);
    
    threadDictionary["threadOne"].Abort();