@@ -9,44 +9,66 @@ namespace PSParallelPipeline;
99
1010internal sealed class PSTask
1111{
12+ private const string SetVariableCommand = "Set-Variable" ;
13+
14+ private const string DollarUnderbar = "_" ;
15+
16+ private const string StopParsingOp = "--%" ;
17+
1218 private readonly PowerShell _powershell ;
1319
1420 private readonly PSDataStreams _internalStreams ;
1521
16- private readonly RunspacePool _pool ;
22+ private Runspace ? _runspace ;
1723
18- private PSOutputStreams OutputStreams { get => _pool . Streams ; }
24+ private readonly PSOutputStreams _outputStreams ;
1925
20- private Runspace Runspace
21- {
22- get => _powershell . Runspace ;
23- set => _powershell . Runspace = value ;
24- }
26+ private readonly CancellationToken _token ;
27+
28+ private readonly RunspacePool _pool ;
2529
2630 private PSTask ( RunspacePool pool )
2731 {
2832 _powershell = PowerShell . Create ( ) ;
2933 _internalStreams = _powershell . Streams ;
34+ _outputStreams = pool . Streams ;
35+ _token = pool . Token ;
3036 _pool = pool ;
3137 }
3238
33- static internal async Task < PSTask > CreateAsync (
39+ static internal PSTask Create (
3440 object ? input ,
3541 RunspacePool runspacePool ,
3642 TaskSettings settings )
3743 {
3844 PSTask ps = new ( runspacePool ) ;
3945 SetStreams ( ps . _internalStreams , runspacePool . Streams ) ;
40- ps . Runspace = await runspacePool
41- . GetRunspaceAsync ( )
42- . ConfigureAwait ( false ) ;
4346
4447 return ps
4548 . AddInput ( input )
4649 . AddScript ( settings . Script )
4750 . AddUsingStatements ( settings . UsingStatements ) ;
4851 }
4952
53+ internal async Task InvokeAsync ( )
54+ {
55+ try
56+ {
57+ using CancellationTokenRegistration _ = _token . Register ( Cancel ) ;
58+ _runspace = await _pool . GetRunspaceAsync ( ) . ConfigureAwait ( false ) ;
59+ _powershell . Runspace = _runspace ;
60+ await InvokePowerShellAsync ( _powershell , _outputStreams . Success ) . ConfigureAwait ( false ) ;
61+ }
62+ catch ( Exception exception )
63+ {
64+ _outputStreams . AddOutput ( exception . CreateProcessingTaskError ( this ) ) ;
65+ }
66+ finally
67+ {
68+ CompleteTask ( ) ;
69+ }
70+ }
71+
5072 private static void SetStreams (
5173 PSDataStreams streams ,
5274 PSOutputStreams outputStreams )
@@ -71,8 +93,8 @@ private PSTask AddInput(object? inputObject)
7193 if ( inputObject is not null )
7294 {
7395 _powershell
74- . AddCommand ( "Set-Variable" , useLocalScope : true )
75- . AddArgument ( "_" )
96+ . AddCommand ( SetVariableCommand , useLocalScope : true )
97+ . AddArgument ( DollarUnderbar )
7698 . AddArgument ( inputObject ) ;
7799 }
78100
@@ -89,33 +111,27 @@ private PSTask AddUsingStatements(Dictionary<string, object?> usingParams)
89111 {
90112 if ( usingParams . Count > 0 )
91113 {
92- _powershell . AddParameter ( "--%" , usingParams ) ;
114+ _powershell . AddParameter ( StopParsingOp , usingParams ) ;
93115 }
94116
95117 return this ;
96118 }
97119
98- internal async Task InvokeAsync ( )
120+ private void CompleteTask ( )
99121 {
100- try
101- {
102- using CancellationTokenRegistration _ = _pool . RegisterCancellation ( Cancel ) ;
103- await InvokePowerShellAsync ( _powershell , OutputStreams . Success ) . ConfigureAwait ( false ) ;
104- }
105- catch ( Exception exception )
106- {
107- OutputStreams . AddOutput ( exception . CreateProcessingTaskError ( this ) ) ;
108- }
109- finally
122+ _powershell . Dispose ( ) ;
123+ if ( ! _token . IsCancellationRequested && _runspace is not null )
110124 {
111- _powershell . Dispose ( ) ;
112- _pool . PushRunspace ( Runspace ) ;
125+ _pool . PushRunspace ( _runspace ) ;
126+ return ;
113127 }
128+
129+ _runspace ? . Dispose ( ) ;
114130 }
115131
116132 private void Cancel ( )
117133 {
118134 _powershell . Dispose ( ) ;
119- Runspace . Dispose ( ) ;
135+ _runspace ? . Dispose ( ) ;
120136 }
121137}
0 commit comments