【发布时间】:2018-10-10 07:38:59
【问题描述】:
我试图理解对重载方法 Task.Run(Func, CancellationToken) 的需求。
为什么还存在CancelationToken类型的第二个参数,如果它甚至没有传递到Task.Run()方法的第一个参数中指定的函数中?我不得不在sum 函数代码中为此使用闭包。此外,我在 Microsoft 的代码示例中也看到了相同的内容(请查看上面的链接)。
这是我的代码:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ThreadsLearning {
class Program {
private static void Main(string[] args) {
CancellationTokenSource tokenSrc = new CancellationTokenSource();
Func<int, int> sum = n => {
int result = 0;
for (var i = 0; i <= n; i++) {
result += i;
tokenSrc.Token.ThrowIfCancellationRequested();
Console.WriteLine(result);
Thread.Sleep(200);
}
return result;
};
/* Why does exist a second parameter of CancelationToken type, if it
* is even not passed into the function specified in the first parameter
* of Task.Run() method?
* I'm forced to use a closure for this in the 'sum' function code.
*/
var task = Task.Run(() => sum(30), tokenSrc.Token);
// I will get the seame result for CancellationToken.None:
// var task = Task.Run(() => sum(30), CancellationToken.None);
tokenSrc.CancelAfter(TimeSpan.FromSeconds(2));
try {
Console.WriteLine("Result: {0}", task.Result);
}
catch (AggregateException ex) {
foreach (var item in ex.InnerExceptions) {
Console.WriteLine("Exception: {0}", item.Message);
}
}
catch (Exception ex) {
Console.WriteLine("Other exception (the other 'catch' block): {0}", ex.Message);
}
Console.WriteLine("Hit <Enter> for exit...");
Console.ReadLine();
}
}
}
【问题讨论】:
-
您链接到的备注的第一句 - “如果在任务开始执行之前请求取消,任务不会执行。相反,它被设置为已取消状态并且引发 TaskCanceledException 异常。” (我的重点)
标签: c# .net scheduled-tasks