@@ -91,7 +91,7 @@ bool IPSConsoleReadLineMockableMethods.RunspaceIsRemote(Runspace runspace)
91
91
return runspace ? . ConnectionInfo != null ;
92
92
}
93
93
94
- private void ReadOneOrMoreKeys ( CancellationToken cancellationToken )
94
+ private void ReadOneOrMoreKeys ( )
95
95
{
96
96
_readkeyStopwatch . Restart ( ) ;
97
97
while ( _console . KeyAvailable )
@@ -143,15 +143,17 @@ private void ReadOneOrMoreKeys(CancellationToken cancellationToken)
143
143
}
144
144
while ( _charMap . KeyAvailable )
145
145
{
146
- var key = PSKeyInfo . FromConsoleKeyInfo ( _charMap . ReadKey ( ) ) ;
147
- if ( cancellationToken . IsCancellationRequested )
146
+ ConsoleKeyInfo keyInfo = _charMap . ReadKey ( ) ;
147
+ if ( _cancelReadCancellationToken . IsCancellationRequested )
148
148
{
149
149
// If PSReadLine is running under a host that can cancel it, the
150
150
// cancellation will come at a time when ReadKey is stuck waiting for input.
151
151
// The next key press will be used to force it to return, and so we want to
152
152
// discard this key since we were already canceled.
153
153
continue ;
154
154
}
155
+
156
+ var key = PSKeyInfo . FromConsoleKeyInfo ( keyInfo ) ;
155
157
_lastNKeys . Enqueue ( key ) ;
156
158
_queuedKeys . Enqueue ( key ) ;
157
159
}
@@ -163,13 +165,12 @@ private void ReadKeyThreadProc()
163
165
while ( true )
164
166
{
165
167
// Wait until ReadKey tells us to read a key (or it's time to exit).
166
- int handleId = WaitHandle . WaitAny ( _singleton . _threadProcWaitHandles ) ;
168
+ int handleId = WaitHandle . WaitAny ( _threadProcWaitHandles ) ;
167
169
if ( handleId == 1 ) // It was the _closingWaitHandle that was signaled.
168
170
break ;
169
171
170
- var localCancellationToken = _singleton . _cancelReadCancellationToken ;
171
- ReadOneOrMoreKeys ( localCancellationToken ) ;
172
- if ( localCancellationToken . IsCancellationRequested )
172
+ ReadOneOrMoreKeys ( ) ;
173
+ if ( _cancelReadCancellationToken . IsCancellationRequested )
173
174
{
174
175
continue ;
175
176
}
@@ -395,7 +396,7 @@ public static string ReadLine(
395
396
}
396
397
397
398
_singleton . _cancelReadCancellationToken = cancellationToken ;
398
- _singleton . _requestKeyWaitHandles [ 2 ] = _singleton . _cancelReadCancellationToken . WaitHandle ;
399
+ _singleton . _requestKeyWaitHandles [ 2 ] = cancellationToken . WaitHandle ;
399
400
return _singleton . InputLoop ( ) ;
400
401
}
401
402
catch ( OperationCanceledException )
@@ -873,11 +874,11 @@ private void DelayedOneTimeInitialize()
873
874
_killIndex = - 1 ; // So first add indexes 0.
874
875
_killRing = new List < string > ( Options . MaximumKillRingCount ) ;
875
876
876
- _singleton . _readKeyWaitHandle = new AutoResetEvent ( false ) ;
877
- _singleton . _keyReadWaitHandle = new AutoResetEvent ( false ) ;
878
- _singleton . _closingWaitHandle = new ManualResetEvent ( false ) ;
879
- _singleton . _requestKeyWaitHandles = new WaitHandle [ ] { _singleton . _keyReadWaitHandle , _singleton . _closingWaitHandle , _defaultCancellationToken . WaitHandle } ;
880
- _singleton . _threadProcWaitHandles = new WaitHandle [ ] { _singleton . _readKeyWaitHandle , _singleton . _closingWaitHandle } ;
877
+ _readKeyWaitHandle = new AutoResetEvent ( false ) ;
878
+ _keyReadWaitHandle = new AutoResetEvent ( false ) ;
879
+ _closingWaitHandle = new ManualResetEvent ( false ) ;
880
+ _requestKeyWaitHandles = new WaitHandle [ ] { _keyReadWaitHandle , _closingWaitHandle , null } ;
881
+ _threadProcWaitHandles = new WaitHandle [ ] { _readKeyWaitHandle , _closingWaitHandle } ;
881
882
882
883
// This is for a "being hosted in an alternate appdomain scenario" (the
883
884
// DomainUnload event is not raised for the default appdomain). It allows us
@@ -887,13 +888,13 @@ private void DelayedOneTimeInitialize()
887
888
{
888
889
AppDomain . CurrentDomain . DomainUnload += ( x , y ) =>
889
890
{
890
- _singleton . _closingWaitHandle . Set ( ) ;
891
- _singleton . _readKeyThread . Join ( ) ; // may need to wait for history to be written
891
+ _closingWaitHandle . Set ( ) ;
892
+ _readKeyThread . Join ( ) ; // may need to wait for history to be written
892
893
} ;
893
894
}
894
895
895
- _singleton . _readKeyThread = new Thread ( _singleton . ReadKeyThreadProc ) { IsBackground = true , Name = "PSReadLine ReadKey Thread" } ;
896
- _singleton . _readKeyThread . Start ( ) ;
896
+ _readKeyThread = new Thread ( ReadKeyThreadProc ) { IsBackground = true , Name = "PSReadLine ReadKey Thread" } ;
897
+ _readKeyThread . Start ( ) ;
897
898
}
898
899
899
900
private static void Chord ( ConsoleKeyInfo ? key = null , object arg = null )
0 commit comments