Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 8ac24f5

Browse files
committed
Make sure finally always gets called
1 parent 690dec2 commit 8ac24f5

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/GitHub.Api/Tasks/TaskBase.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,17 @@ public ITask Finally<T>(T taskToContinueWith)
213213
return continuationOnAlways;
214214
}
215215

216+
/// <summary>
217+
/// This does not set a dependency between the two tasks. Instead,
218+
/// the Start method grabs the state of the previous task to pass on
219+
/// to the next task via previousSuccess and previousException
220+
/// </summary>
221+
/// <param name="handler"></param>
216222
internal void SetFaultHandler(TaskBase handler)
217223
{
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));
224+
Task.ContinueWith(t => handler.Start(t), Token,
225+
TaskContinuationOptions.OnlyOnFaulted,
226+
TaskManager.GetScheduler(handler.Affinity));
224227
DependsOn?.SetFaultHandler(handler);
225228
}
226229

@@ -260,6 +263,11 @@ protected void Run()
260263
}
261264
}
262265

266+
/// <summary>
267+
/// Call this to run a task after another task is done, without
268+
/// having them depend on each other
269+
/// </summary>
270+
/// <param name="task"></param>
263271
protected void Start(Task task)
264272
{
265273
previousSuccess = task.Status == TaskStatus.RanToCompletion && task.Status != TaskStatus.Faulted;
@@ -365,6 +373,11 @@ protected virtual void RaiseOnEnd()
365373
//Logger.Trace($"Finished {ToString()}");
366374
}
367375

376+
protected void CallFinallyHandler()
377+
{
378+
finallyHandler?.Invoke();
379+
}
380+
368381
protected virtual bool RaiseFaultHandlers(Exception ex)
369382
{
370383
if (catchHandler == null)
@@ -565,7 +578,10 @@ protected virtual void RaiseOnEnd(TResult result)
565578
{
566579
OnEnd?.Invoke(this, result);
567580
if (continuationOnSuccess == null && continuationOnFailure == null && continuationOnAlways == null)
581+
{
568582
finallyHandler?.Invoke(result);
583+
CallFinallyHandler();
584+
}
569585
//Logger.Trace($"Finished {ToString()} {result}");
570586
}
571587

0 commit comments

Comments
 (0)