@@ -519,9 +519,11 @@ public Task<RunspaceHandle> GetRunspaceHandleAsync(CancellationToken cancellatio
519
519
public Task < IEnumerable < TResult > > ExecuteCommandAsync < TResult > (
520
520
PSCommand psCommand ,
521
521
bool sendOutputToHost = false ,
522
- bool sendErrorToHost = true )
522
+ bool sendErrorToHost = true ,
523
+ CancellationToken cancellationToken = default )
523
524
{
524
- return ExecuteCommandAsync < TResult > ( psCommand , errorMessages : null , sendOutputToHost , sendErrorToHost ) ;
525
+ return this . ExecuteCommandAsync < TResult > (
526
+ psCommand , errorMessages : null , sendOutputToHost , sendErrorToHost , cancellationToken : cancellationToken ) ;
525
527
}
526
528
527
529
/// <summary>
@@ -549,7 +551,8 @@ public Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
549
551
StringBuilder errorMessages ,
550
552
bool sendOutputToHost = false ,
551
553
bool sendErrorToHost = true ,
552
- bool addToHistory = false )
554
+ bool addToHistory = false ,
555
+ CancellationToken cancellationToken = default )
553
556
{
554
557
return
555
558
this . ExecuteCommandAsync < TResult > (
@@ -560,7 +563,8 @@ public Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
560
563
WriteOutputToHost = sendOutputToHost ,
561
564
WriteErrorsToHost = sendErrorToHost ,
562
565
AddToHistory = addToHistory
563
- } ) ;
566
+ } ,
567
+ cancellationToken ) ;
564
568
}
565
569
566
570
@@ -581,7 +585,8 @@ public Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
581
585
public async Task < IEnumerable < TResult > > ExecuteCommandAsync < TResult > (
582
586
PSCommand psCommand ,
583
587
StringBuilder errorMessages ,
584
- ExecutionOptions executionOptions )
588
+ ExecutionOptions executionOptions ,
589
+ CancellationToken cancellationToken = default )
585
590
{
586
591
Validate . IsNotNull ( nameof ( psCommand ) , psCommand ) ;
587
592
Validate . IsNotNull ( nameof ( executionOptions ) , executionOptions ) ;
@@ -759,8 +764,11 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
759
764
return shell . Invoke < TResult > ( null , invocationSettings ) ;
760
765
}
761
766
762
- // May need a cancellation token here
763
- return await Task . Run < IEnumerable < TResult > > ( ( ) => shell . Invoke < TResult > ( input : null , invocationSettings ) , CancellationToken . None ) . ConfigureAwait ( false ) ;
767
+ // This is the primary reason that ExecuteCommandAsync takes a CancellationToken
768
+ cancellationToken . Register ( ( ) => shell . Stop ( ) ) ;
769
+ return await Task . Run < IEnumerable < TResult > > (
770
+ ( ) => shell . Invoke < TResult > ( input : null , invocationSettings ) , cancellationToken )
771
+ . ConfigureAwait ( false ) ;
764
772
}
765
773
finally
766
774
{
@@ -872,7 +880,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
872
880
// will exist already so we need to create one and then use it
873
881
if ( runspaceHandle == null )
874
882
{
875
- runspaceHandle = await this . GetRunspaceHandleAsync ( ) . ConfigureAwait ( false ) ;
883
+ runspaceHandle = await this . GetRunspaceHandleAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
876
884
}
877
885
878
886
sessionDetails = this . GetSessionDetailsInRunspace ( runspaceHandle . Runspace ) ;
0 commit comments