问题描述
示例Ref 70-483
var cancelationTokenSource = new CancellationTokenSource();
            var token = cancelationTokenSource.Token;
          var task = Task.Run(()=>
{
;   而(token.IsCancellationRequested!)
         {
 &NBSP ;         Console.WriteLine(QUOT * QUOT);
        Thread.Sleep(1000);
}
       token.ThrowIfCancellationRequested();
      },令牌)
          .ContinueWith((t)=>
{
t.Exception.Handle ((E)=> 真);     强>
        控制台。的WriteLine(t.Status);
      },TaskContinuationOptions.OnlyOnCanceled);
            Console.WriteLine(" Cancel Enter");
          Console.ReadLine();
          cancelationTokenSource.Cancel();
          Console.WriteLine(" Enter");
          Console.ReadLine();
                       
Example Ref 70-483
var cancelationTokenSource = new CancellationTokenSource();
var token = cancelationTokenSource.Token;
var task = Task.Run(() =>
{
while (!token.IsCancellationRequested)
{
Console.WriteLine("*");
Thread.Sleep(1000);
}
token.ThrowIfCancellationRequested();
}, token)
.ContinueWith((t) =>
{
t.Exception.Handle((e) => true);
Console.WriteLine(t.Status);
}, TaskContinuationOptions.OnlyOnCanceled);
Console.WriteLine("Cancel Enter");
Console.ReadLine();
cancelationTokenSource.Cancel();
Console.WriteLine(" Enter");
Console.ReadLine();
这篇关于为什么属性Exception为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!