mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Update the task-based async APIs added to PowerShell to return a Task object directly (#9079)
Update the task-based async APIs added to PowerShell to not use the `aysnc/await` keywords, but to return a `Task` object directly. There is nothing to continue on after the `Task.Factory.FromAsync` call in those methods, so there is not need to use `aysnc` and `await` keywords, which turns the method into a state machine class unnecessarily.
This commit is contained in:
@@ -3083,10 +3083,8 @@ namespace System.Management.Automation
|
||||
/// <exception cref="ObjectDisposedException">
|
||||
/// Object is disposed.
|
||||
/// </exception>
|
||||
public async Task<PSDataCollection<PSObject>> InvokeAsync()
|
||||
{
|
||||
return await Task<PSDataCollection<PSObject>>.Factory.FromAsync(BeginInvoke(), _endInvokeMethod).ConfigureAwait(false);
|
||||
}
|
||||
public Task<PSDataCollection<PSObject>> InvokeAsync()
|
||||
=> Task<PSDataCollection<PSObject>>.Factory.FromAsync(BeginInvoke(), _endInvokeMethod);
|
||||
|
||||
/// <summary>
|
||||
/// Invoke a PowerShell command asynchronously.
|
||||
@@ -3126,8 +3124,8 @@ namespace System.Management.Automation
|
||||
/// <exception cref="ObjectDisposedException">
|
||||
/// Object is disposed.
|
||||
/// </exception>
|
||||
public async Task<PSDataCollection<PSObject>> InvokeAsync<T>(PSDataCollection<T> input)
|
||||
=> await Task<PSDataCollection<PSObject>>.Factory.FromAsync(BeginInvoke<T>(input), _endInvokeMethod).ConfigureAwait(false);
|
||||
public Task<PSDataCollection<PSObject>> InvokeAsync<T>(PSDataCollection<T> input)
|
||||
=> Task<PSDataCollection<PSObject>>.Factory.FromAsync(BeginInvoke<T>(input), _endInvokeMethod);
|
||||
|
||||
/// <summary>
|
||||
/// Invoke a PowerShell command asynchronously.
|
||||
@@ -3177,8 +3175,8 @@ namespace System.Management.Automation
|
||||
/// <exception cref="ObjectDisposedException">
|
||||
/// Object is disposed.
|
||||
/// </exception>
|
||||
public async Task<PSDataCollection<PSObject>> InvokeAsync<T>(PSDataCollection<T> input, PSInvocationSettings settings, AsyncCallback callback, object state)
|
||||
=> await Task<PSDataCollection<PSObject>>.Factory.FromAsync(BeginInvoke<T>(input, settings, callback, state), _endInvokeMethod).ConfigureAwait(false);
|
||||
public Task<PSDataCollection<PSObject>> InvokeAsync<T>(PSDataCollection<T> input, PSInvocationSettings settings, AsyncCallback callback, object state)
|
||||
=> Task<PSDataCollection<PSObject>>.Factory.FromAsync(BeginInvoke<T>(input, settings, callback, state), _endInvokeMethod);
|
||||
|
||||
/// <summary>
|
||||
/// Invoke a PowerShell command asynchronously.
|
||||
@@ -3225,8 +3223,8 @@ namespace System.Management.Automation
|
||||
/// <exception cref="ObjectDisposedException">
|
||||
/// Object is disposed.
|
||||
/// </exception>
|
||||
public async Task<PSDataCollection<PSObject>> InvokeAsync<TInput, TOutput>(PSDataCollection<TInput> input, PSDataCollection<TOutput> output)
|
||||
=> await Task<PSDataCollection<PSObject>>.Factory.FromAsync(BeginInvoke<TInput, TOutput>(input, output), _endInvokeMethod).ConfigureAwait(false);
|
||||
public Task<PSDataCollection<PSObject>> InvokeAsync<TInput, TOutput>(PSDataCollection<TInput> input, PSDataCollection<TOutput> output)
|
||||
=> Task<PSDataCollection<PSObject>>.Factory.FromAsync(BeginInvoke<TInput, TOutput>(input, output), _endInvokeMethod);
|
||||
|
||||
/// <summary>
|
||||
/// Invoke a PowerShell command asynchronously and collect
|
||||
@@ -3284,8 +3282,8 @@ namespace System.Management.Automation
|
||||
/// <exception cref="ObjectDisposedException">
|
||||
/// Object is disposed.
|
||||
/// </exception>
|
||||
public async Task<PSDataCollection<PSObject>> InvokeAsync<TInput, TOutput>(PSDataCollection<TInput> input, PSDataCollection<TOutput> output, PSInvocationSettings settings, AsyncCallback callback, object state)
|
||||
=> await Task<PSDataCollection<PSObject>>.Factory.FromAsync(BeginInvoke<TInput, TOutput>(input, output, settings, callback, state), _endInvokeMethod).ConfigureAwait(false);
|
||||
public Task<PSDataCollection<PSObject>> InvokeAsync<TInput, TOutput>(PSDataCollection<TInput> input, PSDataCollection<TOutput> output, PSInvocationSettings settings, AsyncCallback callback, object state)
|
||||
=> Task<PSDataCollection<PSObject>>.Factory.FromAsync(BeginInvoke<TInput, TOutput>(input, output, settings, callback, state), _endInvokeMethod);
|
||||
|
||||
/// <summary>
|
||||
/// Begins a batch execution.
|
||||
@@ -3808,10 +3806,8 @@ namespace System.Management.Automation
|
||||
/// <exception cref="ObjectDisposedException">
|
||||
/// Object is disposed.
|
||||
/// </exception>
|
||||
public async Task StopAsync(AsyncCallback callback, object state)
|
||||
{
|
||||
await Task.Factory.FromAsync(BeginStop(callback, state), _endStopMethod).ConfigureAwait(false);
|
||||
}
|
||||
public Task StopAsync(AsyncCallback callback, object state)
|
||||
=> Task.Factory.FromAsync(BeginStop(callback, state), _endStopMethod);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -84,15 +84,16 @@ try {
|
||||
It 'cannot invoke a single script asynchronously in a runspace that has not been opened' {
|
||||
$rs = [runspacefactory]::CreateRunspace()
|
||||
$ps = [powershell]::Create($rs)
|
||||
|
||||
try {
|
||||
$r = $ps.AddScript('@(1..10).foreach{Start-Sleep -Milliseconds 500}').InvokeAsync()
|
||||
# This test is designed to fail. You cannot invoke PowerShell asynchronously
|
||||
# in a runspace that has not been opened.
|
||||
$r.IsFaulted | Should -Be $true
|
||||
$r.Exception -is [System.AggregateException] | Should -Be $true
|
||||
$r.Exception.InnerException -is [System.Management.Automation.Runspaces.InvalidRunspaceStateException] | Should -Be $true
|
||||
$r.Exception.InnerException.CurrentState | Should -Be 'BeforeOpen'
|
||||
$r.Exception.InnerException.ExpectedState | Should -Be 'Opened'
|
||||
$err = { $ps.AddScript('1+1').InvokeAsync() } | Should -Throw -ErrorId "InvalidRunspaceStateException" -PassThru
|
||||
|
||||
$err.Exception | Should -BeOfType "System.Management.Automation.MethodInvocationException"
|
||||
$err.Exception.InnerException | Should -BeOfType "System.Management.Automation.Runspaces.InvalidRunspaceStateException"
|
||||
$err.Exception.InnerException.CurrentState | Should -Be 'BeforeOpen'
|
||||
$err.Exception.InnerException.ExpectedState | Should -Be 'Opened'
|
||||
} finally {
|
||||
$ps.Dispose()
|
||||
$rs.Dispose()
|
||||
@@ -117,7 +118,7 @@ try {
|
||||
# This test is designed to fail. You cannot invoke PowerShell asynchronously
|
||||
# in the current runspace because nested PowerShell instances cannot be
|
||||
# invoked asynchronously
|
||||
$err = { InvokeAsyncHelper -PowerShell $ps -Wait } | Should -Throw -ErrorId 'AggregateException' -PassThru
|
||||
$err = { $ps.AddScript('1+1').InvokeAsync() } | Should -Throw -ErrorId 'PSInvalidOperationException' -PassThru
|
||||
GetInnerErrorId -Exception $err.Exception | Should -Be 'InvalidOperation'
|
||||
} finally {
|
||||
$ps.Dispose()
|
||||
|
||||
Reference in New Issue
Block a user