commit 414cd9aca99e3e6ec1925372b233605717fe67cf
parent 8f425df48bd55d3fdd4b5c765475506219610520
Author: JohnMcPMS <johnmcp@microsoft.com>
Date: Thu, 25 Jul 2024 13:25:40 -0700
Use ContinueWith rather than finally for coroutine result types (#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.
Diffstat:
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/src/PowerShell/CommonFiles/PowerShellCmdlet.cs b/src/PowerShell/CommonFiles/PowerShellCmdlet.cs
@@ -97,11 +97,14 @@ namespace Microsoft.WinGet.Common.Command
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;
}
}
@@ -150,11 +153,14 @@ namespace Microsoft.WinGet.Common.Command
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;
}
}