为什么属性Exception为null? [英] Why property Exception is null?

查看:801
本文介绍了为什么属性Exception为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例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();


                         

解决方案

ContinueWith
。 延续仅在取消时说明。取消令牌时会触发取消当前任务的OperationCanceledException。然后调用继续,并将取消的任务传递给
。取消时抛出异常以终止未设置为Exception的当前函数。所以当你进入延续时,那么Exception就是null。


事实上,延续并没有真正改变这里的任何东西。如果您取消任务,则

例外
属性未设置。未等待的任务(例如await,Result,Wait)但具有非null Exception属性会在终结器线程上抛出该异常。因此,取消任务会导致意外错误。所以取消任务并不是
设置属性。如果您查看Wait的代码,它会检查已取消的任务并抛出(通常看到的)OperationCancelledException。如果任务尚未被取消,那么它将查看Exception属性以查看是否需要抛出。


总之,只有在任务抛出异常时才设置Exception。取消和成功完成不会设置此属性。


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();

                         

解决方案

ContinueWith is called after the task completes.  The continuation says only on cancellation. When the token is cancelled that triggers an OperationCanceledException which cancels the current task. The continuation is then called and the cancelled task is passed to it. While the cancellation throws an exception to terminate the current function that isn't set as the Exception. So when you get to the continuation then Exception is null.

In fact that continuation doesn't really change anything here. If you cancel a task then the Exception property isn't set. Tasks that aren't awaited (e.g. await, Result, Wait) but have a non-null Exception property have that exception thrown on the finalizer thread. So cancelling a task would cause unexpected errors. So cancelling a task doesn't set the property. If you look at the code for Wait it checks for a cancelled task and throws the (normally seen) OperationCancelledException. If the task hasn't been cancelled then it looks at the Exception property to see if it needs to throw.

In summary, Exception is only set if the task throws an exception. Cancellation and successful completion do not set this property.


这篇关于为什么属性Exception为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆