公司网站管理属于什么职位,零基础学电脑的自学软件,七牛云cdn wordpress,mui做浏览器网站跳转咨询区 Patrice Pezillier#xff1a;我在一个线程里创建了若干了task并开启执行#xff0c;当我在业务逻辑中执行了 Thread.Abort() 之后#xff0c;我发现这些 Task 并没有被终止掉#xff1f;问题来了#xff0c;我如何将 Abort() 传递到内部的 Task 呢#xff1f;回答… 咨询区 Patrice Pezillier我在一个线程里创建了若干了task并开启执行当我在业务逻辑中执行了 Thread.Abort() 之后我发现这些 Task 并没有被终止掉问题来了我如何将 Abort() 传递到内部的 Task 呢回答区 Darin Dimitrov你做不到的Task默认就依托于底层线程池中的线程同时用 Thread.Abort() 来中止线程也是不推荐的在 Task 中推荐的做法是使用 cancellation tokens可参考如下代码
class Program
{static void Main(){var ts new CancellationTokenSource();CancellationToken ct ts.Token;Task.Factory.StartNew(() {while (true){// do some heavy work hereThread.Sleep(100);if (ct.IsCancellationRequested){// another thread decided to cancelConsole.WriteLine(task canceled);break;}}}, ct);// Simulate waiting 3s for the task to completeThread.Sleep(3000);// Cant wait anymore cancel this task ts.Cancel();Console.ReadLine();}
}Florian Rappl之所以在中止Thread后Task没有被终止的原因在于你在Task中没有捕获到当前的 Thread 如果做到了这点那就可以完美解决了参考如下代码
void Main()
{Thread thread null;Task t Task.Run(() {//Capture the threadthread Thread.CurrentThread;//Simulate work (usually from 3rd party code)Thread.Sleep(1000);//If you comment out thread.Abort(), then this will be displayedConsole.WriteLine(Task finished!);});//This is needed in the example to avoid thread being still NULLThread.Sleep(10);//Cancel the task by aborting the threadthread.Abort();
}点评区 其实这是大家用多线程开发必然会遇到的一个问题对 CancellationToken 的理解和运用还是非常重要的。
相关文章: