【发布时间】:2011-03-14 13:19:18
【问题描述】:
假设,有一个任务大致包含以下动作:
Task someTask = new Task(() => {
while(!IsCancellationRequested) {
Do_something_over_a_long_period_of_time();
token.ThrowIfCancellationRequested();
Do_something_over_a_long_period_of_time();
token.ThrowIfCancellationRequested();
Do_something_over_a_long_period_of_time();
token.ThrowIfCancellationRequested();
}
});
someTask.Start();
而且有相当不耐烦的用户。他们渴望立即终止我的申请。他们不想在长时间的操作运行时等待。
我曾经使用Thread 类,并且能够通过调用Abort() 命令立即中止我的所有线程。
如何立即中止我的任务?
谢谢。
【问题讨论】:
-
你看过BackgroundWorker吗?
-
@HadleyHope Background Worker 如何解决他的问题。它支持与任务类似的取消方式
标签: c# multithreading task task-parallel-library