当前位置: 首页 > news >正文

做微信号公众号用网站还是App万网域名注册备案

做微信号公众号用网站还是App,万网域名注册备案,西安 美院 网站建设,单页网站制作 在线 支付咨询区 Patrice Pezillier:我在一个线程里创建了若干了task并开启执行,当我在业务逻辑中执行了 Thread.Abort() 之后,我发现这些 Task 并没有被终止掉?问题来了,我如何将 Abort() 传递到内部的 Task 呢?回答…

咨询区

  • 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);// Can't 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 的理解和运用还是非常重要的。

http://www.lebaoying.cn/news/7.html

相关文章: