Question

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

Was it helpful?

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();

OTHER TIPS

I'm not sure what you mean. Do you want to abort the thread in another method later? In that case this should work:

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

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

public void blub() {
   myThread.Abort();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top