1
1
using GitHub . Logging ;
2
2
using System ;
3
+ using System . Collections . Generic ;
3
4
using System . Threading ;
4
5
using System . Threading . Tasks ;
5
6
@@ -160,6 +161,7 @@ public virtual T Then<T>(T nextTask, TaskRunOptions runOptions = TaskRunOptions.
160
161
nextTaskBase = nextTaskBase . GetTopMostTask ( ) ?? nextTaskBase ;
161
162
// make the next task dependent on this one so it can get values from us
162
163
nextTaskBase . SetDependsOn ( this ) ;
164
+ var nextTaskFinallyHandler = nextTaskBase . finallyHandler ;
163
165
164
166
if ( runOptions == TaskRunOptions . OnSuccess )
165
167
{
@@ -173,8 +175,8 @@ public virtual T Then<T>(T nextTask, TaskRunOptions runOptions = TaskRunOptions.
173
175
SetFaultHandler ( nextTaskBase . continuationOnAlways ) ;
174
176
if ( nextTaskBase . catchHandler != null )
175
177
Catch ( nextTaskBase . catchHandler ) ;
176
- if ( nextTaskBase . finallyHandler != null )
177
- Finally ( nextTaskBase . finallyHandler ) ;
178
+ if ( nextTaskFinallyHandler != null )
179
+ Finally ( nextTaskFinallyHandler ) ;
178
180
}
179
181
else if ( runOptions == TaskRunOptions . OnFailure )
180
182
{
@@ -186,6 +188,18 @@ public virtual T Then<T>(T nextTask, TaskRunOptions runOptions = TaskRunOptions.
186
188
this . continuationOnAlways = nextTaskBase ;
187
189
DependsOn ? . SetFaultHandler ( nextTaskBase ) ;
188
190
}
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
+
189
203
return nextTask ;
190
204
}
191
205
@@ -240,11 +254,7 @@ public ITask Finally(Action<bool, Exception> actionToContinueWith, TaskAffinity
240
254
public T Finally < T > ( T taskToContinueWith )
241
255
where T : ITask
242
256
{
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 ) ;
248
258
}
249
259
250
260
/// <summary>
@@ -363,6 +373,15 @@ protected TaskBase GetTopMostTask()
363
373
return depends . GetTopMostTask ( null , false ) ;
364
374
}
365
375
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
+
366
385
protected TaskBase GetTopMostTask ( TaskBase ret , bool onlyCreatedState )
367
386
{
368
387
ret = ( ! onlyCreatedState || Task . Status == TaskStatus . Created ? this : ret ) ;
0 commit comments