@@ -158,10 +158,11 @@ protected void Initialize(Task task)
158158 Token . ThrowIfCancellationRequested ( ) ;
159159 tk . RunSynchronously ( scheduler ) ;
160160 }
161+ else
162+ tk . Wait ( ) ;
161163 }
162164 catch ( Exception ex )
163165 {
164- Errors = ex . Message ;
165166 if ( ! RaiseFaultHandlers ( ex ) )
166167 throw ;
167168 Token . ThrowIfCancellationRequested ( ) ;
@@ -245,11 +246,18 @@ public ITask Catch(Action<Exception> handler)
245246 public ITask Catch ( Func < Exception , bool > handler )
246247 {
247248 Guard . ArgumentNotNull ( handler , "handler" ) ;
248- catchHandler += handler ;
249+ CatchInternal ( handler ) ;
249250 DependsOn ? . Catch ( handler ) ;
250251 return this ;
251252 }
252253
254+ internal ITask CatchInternal ( Func < Exception , bool > handler )
255+ {
256+ Guard . ArgumentNotNull ( handler , "handler" ) ;
257+ catchHandler += handler ;
258+ return this ;
259+ }
260+
253261 /// <summary>
254262 /// Run a callback at the end of the task execution, on the same thread as the task that just finished, regardless of execution state
255263 /// This will always run on the same thread as the previous task
@@ -268,7 +276,14 @@ public ITask Finally(Action<bool> handler)
268276 public ITask Finally ( Action < bool , Exception > actionToContinueWith , TaskAffinity affinity = TaskAffinity . Concurrent )
269277 {
270278 Guard . ArgumentNotNull ( actionToContinueWith , nameof ( actionToContinueWith ) ) ;
271- return Finally ( new ActionTask ( Token , actionToContinueWith ) { Affinity = affinity , Name = "Finally" } ) ;
279+ return Then ( new ActionTask ( Token , ( s , ex ) =>
280+ {
281+ actionToContinueWith ( s , ex ) ;
282+ if ( ! s )
283+ throw ex ;
284+ } )
285+ { Affinity = affinity , Name = "Finally" } , TaskRunOptions . OnAlways )
286+ . CatchInternal ( _ => true ) ;
272287 }
273288
274289 /// <summary>
@@ -438,13 +453,15 @@ protected virtual void RaiseOnStart()
438453
439454 protected virtual bool RaiseFaultHandlers ( Exception ex )
440455 {
456+ exception = ex is AggregateException ? ex . GetBaseException ( ) : ex ;
457+ Errors = exception . Message ;
441458 taskFailed = true ;
442- exception = ex ;
443459 if ( catchHandler == null )
444460 return false ;
461+ var args = new object [ ] { exception } ;
445462 foreach ( var handler in catchHandler . GetInvocationList ( ) )
446463 {
447- if ( ( bool ) handler . DynamicInvoke ( new object [ ] { ex } ) )
464+ if ( ( bool ) handler . DynamicInvoke ( args ) )
448465 {
449466 exceptionWasHandled = true ;
450467 break ;
@@ -497,15 +514,14 @@ protected virtual void CallFinallyHandler()
497514
498515 protected Exception GetThrownException ( )
499516 {
500- if ( DependsOn == null )
501- return null ;
502-
503- if ( DependsOn . Task . Status == TaskStatus . Faulted )
517+ var depends = DependsOn ;
518+ while ( depends != null )
504519 {
505- var ex = DependsOn . Task . Exception ;
506- return ex ? . InnerException ?? ex ;
520+ if ( depends . taskFailed )
521+ return depends . exception ;
522+ depends = depends . DependsOn ;
507523 }
508- return DependsOn . GetThrownException ( ) ;
524+ return null ;
509525 }
510526
511527 public void UpdateProgress ( long value , long total , string message = null )
@@ -583,7 +599,6 @@ protected void Initialize(Task<TResult> task)
583599 }
584600 catch ( Exception ex )
585601 {
586- Errors = ex . Message ;
587602 if ( ! RaiseFaultHandlers ( ex ) )
588603 throw ;
589604 Token . ThrowIfCancellationRequested ( ) ;
@@ -635,7 +650,7 @@ public override T Then<T>(T continuation, TaskRunOptions runOptions = TaskRunOpt
635650 public new ITask < TResult > Catch ( Func < Exception , bool > handler )
636651 {
637652 Guard . ArgumentNotNull ( handler , "handler" ) ;
638- catchHandler += handler ;
653+ CatchInternal ( handler ) ;
639654 DependsOn ? . Catch ( handler ) ;
640655 return this ;
641656 }
@@ -667,7 +682,14 @@ public ITask<TResult> Finally(Func<bool, Exception, TResult, TResult> continuati
667682 public ITask Finally ( Action < bool , Exception , TResult > continuation , TaskAffinity affinity = TaskAffinity . Concurrent )
668683 {
669684 Guard . ArgumentNotNull ( continuation , "continuation" ) ;
670- return Then ( new ActionTask < TResult > ( Token , continuation ) { Affinity = affinity , Name = "Finally" } , TaskRunOptions . OnAlways ) ;
685+ return Then ( new ActionTask < TResult > ( Token , ( s , ex , res ) =>
686+ {
687+ continuation ( s , ex , res ) ;
688+ if ( ! s )
689+ throw ex ;
690+ } )
691+ { Affinity = affinity , Name = "Finally" } , TaskRunOptions . OnAlways )
692+ . CatchInternal ( _ => true ) ;
671693 }
672694
673695 public new ITask < TResult > Start ( )
0 commit comments