Skip to content

Commit

Permalink
Use ContinueWith rather than finally for coroutine result types (micr…
Browse files Browse the repository at this point in the history
…osoft#4669)

## Change
Use `ContinueWith` and `ExecuteSynchronously` to effectively make a
`finally` for the `Task` result object. Convert the existing `finally`
to only call `Complete` on exception.
  • Loading branch information
JohnMcPMS authored Jul 25, 2024
1 parent 8f425df commit 414cd9a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/PowerShell/CommonFiles/PowerShellCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,14 @@ internal Task RunOnMTA(Func<Task> func)
this.Write(StreamType.Verbose, "Already running on MTA");
try
{
return func();
Task result = func();
result.ContinueWith((task) => this.Complete(), TaskContinuationOptions.ExecuteSynchronously);
return result;
}
finally
catch
{
this.Complete();
throw;
}
}

Expand Down Expand Up @@ -150,11 +153,14 @@ internal Task<TResult> RunOnMTA<TResult>(Func<Task<TResult>> func)
this.Write(StreamType.Verbose, "Already running on MTA");
try
{
return func();
Task<TResult> result = func();
result.ContinueWith((task) => this.Complete(), TaskContinuationOptions.ExecuteSynchronously);
return result;
}
finally
catch
{
this.Complete();
throw;
}
}

Expand Down

0 comments on commit 414cd9a

Please sign in to comment.