假设有一个任务包括以下大致操作:
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()
命令立即终止所有线程。
如何立即终止我的任务?
谢谢。