【发布时间】:2017-05-03 20:11:42
【问题描述】:
当这部分代码被执行时
cancellationToken.ThrowIfCancellationRequested();
try catch 块不处理 exception。
public EnumerableObservable(IEnumerable<T> enumerable)
{
this.enumerable = enumerable;
this.cancellationSource = new CancellationTokenSource();
this.cancellationToken = cancellationSource.Token;
this.workerTask = Task.Factory.StartNew(() =>
{
try
{
foreach (var value in this.enumerable)
{
//if task cancellation triggers, raise the proper exception
//to stop task execution
cancellationToken.ThrowIfCancellationRequested();
foreach (var observer in observerList)
{
observer.OnNext(value);
}
}
}
catch (AggregateException e)
{
Console.Write(e.ToString());
}
}, this.cancellationToken);
}
【问题讨论】:
-
赶上
OperationCanceledException
标签: c# task task-parallel-library cancellationtokensource cancellation-token