1 回答
TA贡献1796条经验 获得超4个赞
CancellationToken如果你想单独取消它们,你需要为你开始的每个任务传递一个:
static async Task Main(string[] args)
{
var cancellationSources = Enumerable.Range(0, 3)
.Select(_ => new CancellationTokenSource())
.ToList();
var tasks = Enumerable.Range(0, 3).Select(x => Task.Run(
() => Counter(cancellationSources[x].Token),
cancellationSources[x].Token
));
cancellationSources[1].Cancel();
await Task.WhenAll(tasks);
Console.ReadLine();
}
public static void Counter(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
// or while(true) and token.ThrowIfCancellationRequested(); to throw instead
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i);
}
}
}
- 1 回答
- 0 关注
- 73 浏览
添加回答
举报