@@ -91,7 +91,7 @@ bool IPSConsoleReadLineMockableMethods.RunspaceIsRemote(Runspace runspace)
9191 return runspace ? . ConnectionInfo != null ;
9292 }
9393
94- private void ReadOneOrMoreKeys ( CancellationToken cancellationToken )
94+ private void ReadOneOrMoreKeys ( )
9595 {
9696 _readkeyStopwatch . Restart ( ) ;
9797 while ( _console . KeyAvailable )
@@ -143,15 +143,17 @@ private void ReadOneOrMoreKeys(CancellationToken cancellationToken)
143143 }
144144 while ( _charMap . KeyAvailable )
145145 {
146- var key = PSKeyInfo . FromConsoleKeyInfo ( _charMap . ReadKey ( ) ) ;
147- if ( cancellationToken . IsCancellationRequested )
146+ ConsoleKeyInfo keyInfo = _charMap . ReadKey ( ) ;
147+ if ( _cancelReadCancellationToken . IsCancellationRequested )
148148 {
149149 // If PSReadLine is running under a host that can cancel it, the
150150 // cancellation will come at a time when ReadKey is stuck waiting for input.
151151 // The next key press will be used to force it to return, and so we want to
152152 // discard this key since we were already canceled.
153153 continue ;
154154 }
155+
156+ var key = PSKeyInfo . FromConsoleKeyInfo ( keyInfo ) ;
155157 _lastNKeys . Enqueue ( key ) ;
156158 _queuedKeys . Enqueue ( key ) ;
157159 }
@@ -163,13 +165,12 @@ private void ReadKeyThreadProc()
163165 while ( true )
164166 {
165167 // 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 ) ;
167169 if ( handleId == 1 ) // It was the _closingWaitHandle that was signaled.
168170 break ;
169171
170- var localCancellationToken = _singleton . _cancelReadCancellationToken ;
171- ReadOneOrMoreKeys ( localCancellationToken ) ;
172- if ( localCancellationToken . IsCancellationRequested )
172+ ReadOneOrMoreKeys ( ) ;
173+ if ( _cancelReadCancellationToken . IsCancellationRequested )
173174 {
174175 continue ;
175176 }
@@ -395,7 +396,7 @@ public static string ReadLine(
395396 }
396397
397398 _singleton . _cancelReadCancellationToken = cancellationToken ;
398- _singleton . _requestKeyWaitHandles [ 2 ] = _singleton . _cancelReadCancellationToken . WaitHandle ;
399+ _singleton . _requestKeyWaitHandles [ 2 ] = cancellationToken . WaitHandle ;
399400 return _singleton . InputLoop ( ) ;
400401 }
401402 catch ( OperationCanceledException )
@@ -873,11 +874,11 @@ private void DelayedOneTimeInitialize()
873874 _killIndex = - 1 ; // So first add indexes 0.
874875 _killRing = new List < string > ( Options . MaximumKillRingCount ) ;
875876
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 } ;
881882
882883 // This is for a "being hosted in an alternate appdomain scenario" (the
883884 // DomainUnload event is not raised for the default appdomain). It allows us
@@ -887,13 +888,13 @@ private void DelayedOneTimeInitialize()
887888 {
888889 AppDomain . CurrentDomain . DomainUnload += ( x , y ) =>
889890 {
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
892893 } ;
893894 }
894895
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 ( ) ;
897898 }
898899
899900 private static void Chord ( ConsoleKeyInfo ? key = null , object arg = null )
0 commit comments