11using GitHub . Logging ;
22using System ;
3+ using System . Collections . Generic ;
34using System . Threading ;
45using System . Threading . Tasks ;
56
@@ -160,6 +161,7 @@ public virtual T Then<T>(T nextTask, TaskRunOptions runOptions = TaskRunOptions.
160161 nextTaskBase = nextTaskBase . GetTopMostTask ( ) ?? nextTaskBase ;
161162 // make the next task dependent on this one so it can get values from us
162163 nextTaskBase . SetDependsOn ( this ) ;
164+ var nextTaskFinallyHandler = nextTaskBase . finallyHandler ;
163165
164166 if ( runOptions == TaskRunOptions . OnSuccess )
165167 {
@@ -173,8 +175,8 @@ public virtual T Then<T>(T nextTask, TaskRunOptions runOptions = TaskRunOptions.
173175 SetFaultHandler ( nextTaskBase . continuationOnAlways ) ;
174176 if ( nextTaskBase . catchHandler != null )
175177 Catch ( nextTaskBase . catchHandler ) ;
176- if ( nextTaskBase . finallyHandler != null )
177- Finally ( nextTaskBase . finallyHandler ) ;
178+ if ( nextTaskFinallyHandler != null )
179+ Finally ( nextTaskFinallyHandler ) ;
178180 }
179181 else if ( runOptions == TaskRunOptions . OnFailure )
180182 {
@@ -186,6 +188,18 @@ public virtual T Then<T>(T nextTask, TaskRunOptions runOptions = TaskRunOptions.
186188 this . continuationOnAlways = nextTaskBase ;
187189 DependsOn ? . SetFaultHandler ( nextTaskBase ) ;
188190 }
191+
192+ // if the current task has a fault handler, attach it to the chain we're appending
193+ if ( finallyHandler != null )
194+ {
195+ var endOfChainTask = nextTaskBase . GetBottomMostTask ( ) ;
196+ while ( endOfChainTask != this && endOfChainTask != null )
197+ {
198+ endOfChainTask . finallyHandler += finallyHandler ;
199+ endOfChainTask = endOfChainTask . DependsOn ;
200+ }
201+ }
202+
189203 return nextTask ;
190204 }
191205
@@ -240,11 +254,7 @@ public ITask Finally(Action<bool, Exception> actionToContinueWith, TaskAffinity
240254 public T Finally < T > ( T taskToContinueWith )
241255 where T : ITask
242256 {
243- Guard . ArgumentNotNull ( taskToContinueWith , nameof ( taskToContinueWith ) ) ;
244- continuationOnAlways = ( TaskBase ) ( object ) taskToContinueWith ;
245- continuationOnAlways . SetDependsOn ( this ) ;
246- DependsOn ? . SetFaultHandler ( continuationOnAlways ) ;
247- return taskToContinueWith ;
257+ return Then ( taskToContinueWith , TaskRunOptions . OnAlways ) ;
248258 }
249259
250260 /// <summary>
@@ -363,6 +373,15 @@ protected TaskBase GetTopMostTask()
363373 return depends . GetTopMostTask ( null , false ) ;
364374 }
365375
376+ protected TaskBase GetBottomMostTask ( )
377+ {
378+ if ( continuationOnSuccess != null )
379+ return continuationOnSuccess . GetBottomMostTask ( ) ;
380+ else if ( continuationOnAlways != null )
381+ return continuationOnAlways . GetBottomMostTask ( ) ;
382+ return this ;
383+ }
384+
366385 protected TaskBase GetTopMostTask ( TaskBase ret , bool onlyCreatedState )
367386 {
368387 ret = ( ! onlyCreatedState || Task . Status == TaskStatus . Created ? this : ret ) ;
0 commit comments