代码:
CancellationTokenSource cancelSource = new CancellationTokenSource();
cancelSource.Token.Register(() =>
{
Console.WriteLine(">>This is CancellationToken msg: task is canceled");
});
Task t = new Task(() =>
{
for (int i = 0; i < 10; i++)
{
if (cancelSource.IsCancellationRequested)
{
Console.WriteLine(">>cancellationTokenSource cancel the task");
cancelSource.Token.ThrowIfCancellationRequested();
break;
}
Thread.Sleep(200);
Console.WriteLine("first:{0}", i);
//throw new Exception("Error");
}
}, cancelSource.Token);
这个cancelSource.Token在任务中怎么使用,或者是由谁来使用的,设置或不设置这个参数会有什么影响