Skip to content

Commit 414cd9a

Browse files
authored
Use ContinueWith rather than finally for coroutine result types (microsoft#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.
1 parent 8f425df commit 414cd9a

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/PowerShell/CommonFiles/PowerShellCmdlet.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,14 @@ internal Task RunOnMTA(Func<Task> func)
9797
this.Write(StreamType.Verbose, "Already running on MTA");
9898
try
9999
{
100-
return func();
100+
Task result = func();
101+
result.ContinueWith((task) => this.Complete(), TaskContinuationOptions.ExecuteSynchronously);
102+
return result;
101103
}
102-
finally
104+
catch
103105
{
104106
this.Complete();
107+
throw;
105108
}
106109
}
107110

@@ -150,11 +153,14 @@ internal Task<TResult> RunOnMTA<TResult>(Func<Task<TResult>> func)
150153
this.Write(StreamType.Verbose, "Already running on MTA");
151154
try
152155
{
153-
return func();
156+
Task<TResult> result = func();
157+
result.ContinueWith((task) => this.Complete(), TaskContinuationOptions.ExecuteSynchronously);
158+
return result;
154159
}
155-
finally
160+
catch
156161
{
157162
this.Complete();
163+
throw;
158164
}
159165
}
160166

0 commit comments

Comments
 (0)