@@ -9,7 +9,7 @@ public enum TaskRunOptions
9
9
{
10
10
OnSuccess ,
11
11
OnFailure ,
12
- Always
12
+ OnAlways
13
13
}
14
14
15
15
public interface ITask : IAsyncResult
@@ -72,9 +72,9 @@ public abstract class TaskBase : ITask
72
72
73
73
protected TaskBase continuationOnSuccess ;
74
74
protected TaskBase continuationOnFailure ;
75
- protected TaskBase continuationAlways ;
75
+ protected TaskBase continuationOnAlways ;
76
76
77
- protected event Func < Exception , bool > faultHandler ;
77
+ protected event Func < Exception , bool > catchHandler ;
78
78
private event Action finallyHandler ;
79
79
protected event Action < IProgress > progressHandler ;
80
80
@@ -135,11 +135,29 @@ public virtual T Then<T>(T nextTask, TaskRunOptions runOptions = TaskRunOptions.
135
135
firstTaskBase . SetDependsOn ( this ) ;
136
136
137
137
if ( runOptions == TaskRunOptions . OnSuccess )
138
+ {
138
139
this . continuationOnSuccess = firstTaskBase ;
140
+ // if there are fault handlers in the chain we're appending, propagate them
141
+ // up this chain as well
142
+ if ( firstTaskBase . continuationOnFailure != null )
143
+ SetFaultHandler ( firstTaskBase . continuationOnFailure ) ;
144
+ else if ( firstTaskBase . continuationOnAlways != null )
145
+ SetFaultHandler ( firstTaskBase . continuationOnAlways ) ;
146
+ if ( firstTaskBase . catchHandler != null )
147
+ Catch ( firstTaskBase . catchHandler ) ;
148
+ if ( firstTaskBase . finallyHandler != null )
149
+ Finally ( firstTaskBase . finallyHandler ) ;
150
+ }
139
151
else if ( runOptions == TaskRunOptions . OnFailure )
152
+ {
140
153
this . continuationOnFailure = firstTaskBase ;
154
+ DependsOn ? . SetFaultHandler ( firstTaskBase ) ;
155
+ }
141
156
else
142
- this . continuationAlways = firstTaskBase ;
157
+ {
158
+ this . continuationOnAlways = firstTaskBase ;
159
+ DependsOn ? . SetFaultHandler ( firstTaskBase ) ;
160
+ }
143
161
return nextTask ;
144
162
}
145
163
@@ -150,7 +168,7 @@ public virtual T Then<T>(T nextTask, TaskRunOptions runOptions = TaskRunOptions.
150
168
public ITask Catch ( Action < Exception > handler )
151
169
{
152
170
Guard . ArgumentNotNull ( handler , "handler" ) ;
153
- faultHandler += e => { handler ( e ) ; return false ; } ;
171
+ catchHandler += e => { handler ( e ) ; return false ; } ;
154
172
DependsOn ? . Catch ( handler ) ;
155
173
return this ;
156
174
}
@@ -162,7 +180,7 @@ public ITask Catch(Action<Exception> handler)
162
180
public ITask Catch ( Func < Exception , bool > handler )
163
181
{
164
182
Guard . ArgumentNotNull ( handler , "handler" ) ;
165
- faultHandler += handler ;
183
+ catchHandler += handler ;
166
184
DependsOn ? . Catch ( handler ) ;
167
185
return this ;
168
186
}
@@ -188,10 +206,10 @@ public ITask Finally<T>(T taskToContinueWith)
188
206
where T : ITask
189
207
{
190
208
Guard . ArgumentNotNull ( taskToContinueWith , nameof ( taskToContinueWith ) ) ;
191
- continuationAlways = ( TaskBase ) ( object ) taskToContinueWith ;
192
- continuationAlways . SetDependsOn ( this ) ;
193
- DependsOn ? . SetFaultHandler ( continuationAlways ) ;
194
- return continuationAlways ;
209
+ continuationOnAlways = ( TaskBase ) ( object ) taskToContinueWith ;
210
+ continuationOnAlways . SetDependsOn ( this ) ;
211
+ DependsOn ? . SetFaultHandler ( continuationOnAlways ) ;
212
+ return continuationOnAlways ;
195
213
}
196
214
197
215
internal void SetFaultHandler ( TaskBase handler )
@@ -275,12 +293,12 @@ protected virtual void RunContinuation()
275
293
TaskManager . GetScheduler ( continuationOnFailure . Affinity ) ) ;
276
294
}
277
295
278
- if ( continuationAlways != null )
296
+ if ( continuationOnAlways != null )
279
297
{
280
298
//Logger.Trace($"Setting ContinueWith {Affinity} {continuation}");
281
- Task . ContinueWith ( _ => ( ( TaskBase ) ( object ) continuationAlways ) . Run ( ) , Token ,
299
+ Task . ContinueWith ( _ => ( ( TaskBase ) ( object ) continuationOnAlways ) . Run ( ) , Token ,
282
300
runAlwaysOptions ,
283
- TaskManager . GetScheduler ( continuationAlways . Affinity ) ) ;
301
+ TaskManager . GetScheduler ( continuationOnAlways . Affinity ) ) ;
284
302
}
285
303
}
286
304
@@ -338,17 +356,17 @@ protected virtual void RaiseOnStart()
338
356
protected virtual void RaiseOnEnd ( )
339
357
{
340
358
OnEnd ? . Invoke ( this ) ;
341
- if ( continuationOnSuccess == null && continuationOnFailure == null && continuationAlways == null )
359
+ if ( continuationOnSuccess == null && continuationOnFailure == null && continuationOnAlways == null )
342
360
finallyHandler ? . Invoke ( ) ;
343
361
//Logger.Trace($"Finished {ToString()}");
344
362
}
345
363
346
364
protected virtual bool RaiseFaultHandlers ( Exception ex )
347
365
{
348
- if ( faultHandler == null )
366
+ if ( catchHandler == null )
349
367
return false ;
350
368
bool handled = false ;
351
- foreach ( var handler in faultHandler . GetInvocationList ( ) )
369
+ foreach ( var handler in catchHandler . GetInvocationList ( ) )
352
370
{
353
371
handled |= ( bool ) handler . DynamicInvoke ( new object [ ] { ex } ) ;
354
372
if ( handled )
@@ -459,7 +477,7 @@ public override T Then<T>(T continuation, TaskRunOptions runOptions = TaskRunOpt
459
477
public new ITask < TResult > Catch ( Action < Exception > handler )
460
478
{
461
479
Guard . ArgumentNotNull ( handler , "handler" ) ;
462
- faultHandler += e => { handler ( e ) ; return false ; } ;
480
+ catchHandler += e => { handler ( e ) ; return false ; } ;
463
481
DependsOn ? . Catch ( handler ) ;
464
482
return this ;
465
483
}
@@ -472,7 +490,7 @@ public override T Then<T>(T continuation, TaskRunOptions runOptions = TaskRunOpt
472
490
public new ITask < TResult > Catch ( Func < Exception , bool > handler )
473
491
{
474
492
Guard . ArgumentNotNull ( handler , "handler" ) ;
475
- faultHandler += handler ;
493
+ catchHandler += handler ;
476
494
DependsOn ? . Catch ( handler ) ;
477
495
return this ;
478
496
}
@@ -491,15 +509,15 @@ public ITask<TResult> Finally(Action<TResult> handler)
491
509
public ITask < TResult > Finally ( Func < bool , Exception , TResult , TResult > continuation , TaskAffinity affinity = TaskAffinity . Concurrent )
492
510
{
493
511
Guard . ArgumentNotNull ( continuation , "continuation" ) ;
494
- var ret = Then ( new FuncTask < TResult , TResult > ( Token , continuation ) { Affinity = affinity , Name = "Finally" } , TaskRunOptions . Always ) ;
512
+ var ret = Then ( new FuncTask < TResult , TResult > ( Token , continuation ) { Affinity = affinity , Name = "Finally" } , TaskRunOptions . OnAlways ) ;
495
513
DependsOn ? . SetFaultHandler ( ret ) ;
496
514
return ret ;
497
515
}
498
516
499
517
public ITask Finally ( Action < bool , Exception , TResult > continuation , TaskAffinity affinity = TaskAffinity . Concurrent )
500
518
{
501
519
Guard . ArgumentNotNull ( continuation , "continuation" ) ;
502
- var ret = Then ( new ActionTask < TResult > ( Token , continuation ) { Affinity = affinity , Name = "Finally" } , TaskRunOptions . Always ) ;
520
+ var ret = Then ( new ActionTask < TResult > ( Token , continuation ) { Affinity = affinity , Name = "Finally" } , TaskRunOptions . OnAlways ) ;
503
521
DependsOn ? . SetFaultHandler ( ret ) ;
504
522
return ret ;
505
523
}
@@ -542,7 +560,7 @@ protected override void RaiseOnStart()
542
560
protected virtual void RaiseOnEnd ( TResult result )
543
561
{
544
562
OnEnd ? . Invoke ( this , result ) ;
545
- if ( continuationOnSuccess == null && continuationOnFailure == null && continuationAlways == null )
563
+ if ( continuationOnSuccess == null && continuationOnFailure == null && continuationOnAlways == null )
546
564
finallyHandler ? . Invoke ( result ) ;
547
565
//Logger.Trace($"Finished {ToString()} {result}");
548
566
}
0 commit comments