简体   繁体   English

通过其名称中止一个线程

[英]Aborting a thread via its name

I was wondering if it's possible to Abort (the dirty way) a thread using the threads name? 我想知道是否有可能使用线程名称Abort(脏路)一个线程? 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(); 例如: myThead[4].Abort();

thanks 谢谢

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM