diff --git a/src/PowerShell/CommonFiles/PowerShellCmdlet.cs b/src/PowerShell/CommonFiles/PowerShellCmdlet.cs index bd66db5ef7..1006e2cb58 100644 --- a/src/PowerShell/CommonFiles/PowerShellCmdlet.cs +++ b/src/PowerShell/CommonFiles/PowerShellCmdlet.cs @@ -97,11 +97,14 @@ internal Task RunOnMTA(Func 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; } } @@ -150,11 +153,14 @@ internal Task RunOnMTA(Func> 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; } }