@@ -127,36 +127,37 @@ public virtual T Then<T>(T nextTask, TaskRunOptions runOptions = TaskRunOptions.
127
127
where T : ITask
128
128
{
129
129
Guard . ArgumentNotNull ( nextTask , nameof ( nextTask ) ) ;
130
- var taskBase = ( ( TaskBase ) ( object ) nextTask ) ;
130
+ var nextTaskBase = ( ( TaskBase ) ( object ) nextTask ) ;
131
131
132
- // find the first task of the continuation chain being appended to this task
133
- var firstTaskBase = taskBase . GetTopMostTask ( ) ?? taskBase ;
134
- // set this task as a dependency of the first task of the continuation chain
135
- firstTaskBase . SetDependsOn ( this ) ;
132
+ // find the task at the top of the chain
133
+ nextTaskBase = nextTaskBase . GetTopMostTask ( ) ?? nextTaskBase ;
134
+ // make the next task dependent on this one so it can get values from us
135
+ nextTaskBase . SetDependsOn ( this ) ;
136
136
137
137
if ( runOptions == TaskRunOptions . OnSuccess )
138
138
{
139
- this . continuationOnSuccess = firstTaskBase ;
139
+ this . continuationOnSuccess = nextTaskBase ;
140
+
140
141
// if there are fault handlers in the chain we're appending, propagate them
141
142
// 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 ) ;
143
+ if ( nextTaskBase . continuationOnFailure != null )
144
+ SetFaultHandler ( nextTaskBase . continuationOnFailure ) ;
145
+ else if ( nextTaskBase . continuationOnAlways != null )
146
+ SetFaultHandler ( nextTaskBase . continuationOnAlways ) ;
147
+ if ( nextTaskBase . catchHandler != null )
148
+ Catch ( nextTaskBase . catchHandler ) ;
149
+ if ( nextTaskBase . finallyHandler != null )
150
+ Finally ( nextTaskBase . finallyHandler ) ;
150
151
}
151
152
else if ( runOptions == TaskRunOptions . OnFailure )
152
153
{
153
- this . continuationOnFailure = firstTaskBase ;
154
- DependsOn ? . SetFaultHandler ( firstTaskBase ) ;
154
+ this . continuationOnFailure = nextTaskBase ;
155
+ DependsOn ? . SetFaultHandler ( nextTaskBase ) ;
155
156
}
156
157
else
157
158
{
158
- this . continuationOnAlways = firstTaskBase ;
159
- DependsOn ? . SetFaultHandler ( firstTaskBase ) ;
159
+ this . continuationOnAlways = nextTaskBase ;
160
+ DependsOn ? . SetFaultHandler ( nextTaskBase ) ;
160
161
}
161
162
return nextTask ;
162
163
}
@@ -214,9 +215,12 @@ public ITask Finally<T>(T taskToContinueWith)
214
215
215
216
internal void SetFaultHandler ( TaskBase handler )
216
217
{
217
- Task . ContinueWith ( t => handler . Start ( t ) , Token ,
218
- TaskContinuationOptions . OnlyOnFaulted ,
219
- TaskManager . GetScheduler ( handler . Affinity ) ) ;
218
+ if ( Task . Status == TaskStatus . Created )
219
+ this . continuationOnFailure = handler ;
220
+ else
221
+ Task . ContinueWith ( t => handler . Start ( t ) , Token ,
222
+ TaskContinuationOptions . OnlyOnFaulted ,
223
+ TaskManager . GetScheduler ( handler . Affinity ) ) ;
220
224
DependsOn ? . SetFaultHandler ( handler ) ;
221
225
}
222
226
0 commit comments