杀死一个线程 [英] Killing a thread
问题描述
我从项目我工作的下面的代码片段:
I have the following code fragment from a project I'm working on:
public void Start()
{
Thread t = new Thread(NotifyIfNecessary);
Threads.Add(t);
t.Start();
t.Abort());
}
我要的是线程'T'应该执行方法NotifyIfNecessary并中止该方法已执行完毕之后。在我当前的代码,t.Abort()被称为过早
What I want is that thread 't' should execute the method NotifyIfNecessary and abort only after the method has completed execution. In my current code, t.Abort() gets called prematurely.
推荐答案
由于您正在创建一个新的这是正在引起线程并启动它,然后立即从您刚刚创建它通过调用 Thread.Abort的()
方法的线程杀死它。你并不需要这样做;当 NotifyIfNecessary
已完成执行你的线程将结束。只是删除行 t.Abort();
和你的代码应能正常工作
This is being caused because you're creating a new thread and starting it, then immediately killing it from the thread that you just created it in by calling the Thread.Abort()
method. You don't need to do this; your thread will finish when NotifyIfNecessary
has completed execution. Just remove the line t.Abort();
and your code should work properly.
这篇关于杀死一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!