@@ -35,15 +35,17 @@ public partial class PSConsoleReadLine : IPSConsoleReadLineMockableMethods
35
35
36
36
private const int CancellationRequested = 2 ;
37
37
38
- private const int EventProcessingRequested = 3 ;
39
-
40
38
// *must* be initialized in the static ctor
41
39
// because the static member _clipboard depends upon it
42
40
// for its own initialization
43
41
private static readonly PSConsoleReadLine _singleton ;
44
42
45
43
private static readonly CancellationToken _defaultCancellationToken = new CancellationTokenSource ( ) . Token ;
46
44
45
+ // This is used by PowerShellEditorServices (the backend of the PowerShell VSCode extension)
46
+ // so that it can call PSReadLine from a delegate and not hit nested pipeline issues.
47
+ private static Action < CancellationToken > _handleIdleOverride ;
48
+
47
49
private bool _delayedOneTimeInitCompleted ;
48
50
49
51
private IPSConsoleReadLineMockableMethods _mockableMethods ;
@@ -55,7 +57,6 @@ public partial class PSConsoleReadLine : IPSConsoleReadLineMockableMethods
55
57
private Thread _readKeyThread ;
56
58
private AutoResetEvent _readKeyWaitHandle ;
57
59
private AutoResetEvent _keyReadWaitHandle ;
58
- private AutoResetEvent _forceEventWaitHandle ;
59
60
private CancellationToken _cancelReadCancellationToken ;
60
61
internal ManualResetEvent _closingWaitHandle ;
61
62
private WaitHandle [ ] _threadProcWaitHandles ;
@@ -195,12 +196,19 @@ internal static PSKeyInfo ReadKey()
195
196
// Next, wait for one of three things:
196
197
// - a key is pressed
197
198
// - the console is exiting
198
- // - 300ms - to process events if we're idle
199
- // - processing of events is requested externally
199
+ // - 300ms timeout - to process events if we're idle
200
200
// - ReadLine cancellation is requested externally
201
201
handleId = WaitHandle . WaitAny ( _singleton . _requestKeyWaitHandles , 300 ) ;
202
- if ( handleId != WaitHandle . WaitTimeout && handleId != EventProcessingRequested )
202
+ if ( handleId != WaitHandle . WaitTimeout )
203
+ {
203
204
break ;
205
+ }
206
+
207
+ if ( _handleIdleOverride is not null )
208
+ {
209
+ _handleIdleOverride ( _singleton . _cancelReadCancellationToken ) ;
210
+ continue ;
211
+ }
204
212
205
213
// If we timed out, check for event subscribers (which is just
206
214
// a hint that there might be an event waiting to be processed.)
@@ -310,15 +318,6 @@ public static string ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsi
310
318
return ReadLine ( runspace , engineIntrinsics , _defaultCancellationToken , lastRunStatus ) ;
311
319
}
312
320
313
- /// <summary>
314
- /// Temporary entry point for PowerShell VSCode extension to avoid breaking the existing PSES.
315
- /// PSES will need to move away from this entry point to actually provide information about 'lastRunStatus'.
316
- /// </summary>
317
- public static string ReadLine ( Runspace runspace , EngineIntrinsics engineIntrinsics , CancellationToken cancellationToken )
318
- {
319
- return ReadLine ( runspace , engineIntrinsics , cancellationToken , lastRunStatus : null ) ;
320
- }
321
-
322
321
/// <summary>
323
322
/// Entry point - called by custom PSHost implementations that require the
324
323
/// ability to cancel ReadLine.
@@ -666,10 +665,6 @@ private PSConsoleReadLine()
666
665
_savedCurrentLine = new HistoryItem ( ) ;
667
666
_queuedKeys = new Queue < PSKeyInfo > ( ) ;
668
667
669
- // Initialize this event handler early because it could be used by PowerShell
670
- // Editor Services before 'DelayedOneTimeInitialize' runs.
671
- _forceEventWaitHandle = new AutoResetEvent ( false ) ;
672
-
673
668
string hostName = null ;
674
669
// This works mostly by luck - we're not doing anything to guarantee the constructor for our
675
670
// singleton is called on a thread with a runspace, but it is happening by coincidence.
@@ -860,7 +855,7 @@ private void DelayedOneTimeInitialize()
860
855
_singleton . _readKeyWaitHandle = new AutoResetEvent ( false ) ;
861
856
_singleton . _keyReadWaitHandle = new AutoResetEvent ( false ) ;
862
857
_singleton . _closingWaitHandle = new ManualResetEvent ( false ) ;
863
- _singleton . _requestKeyWaitHandles = new WaitHandle [ ] { _singleton . _keyReadWaitHandle , _singleton . _closingWaitHandle , _defaultCancellationToken . WaitHandle , _singleton . _forceEventWaitHandle } ;
858
+ _singleton . _requestKeyWaitHandles = new WaitHandle [ ] { _singleton . _keyReadWaitHandle , _singleton . _closingWaitHandle , _defaultCancellationToken . WaitHandle } ;
864
859
_singleton . _threadProcWaitHandles = new WaitHandle [ ] { _singleton . _readKeyWaitHandle , _singleton . _closingWaitHandle } ;
865
860
866
861
// This is for a "being hosted in an alternate appdomain scenario" (the
@@ -880,17 +875,6 @@ private void DelayedOneTimeInitialize()
880
875
_singleton . _readKeyThread . Start ( ) ;
881
876
}
882
877
883
- /// <summary>
884
- /// Used by PowerShellEditorServices to force immediate
885
- /// event handling during the <see cref="PSConsoleReadLine.ReadKey" />
886
- /// method. This is not a public API, but it is part of a private contract
887
- /// with that project.
888
- /// </summary>
889
- private static void ForcePSEventHandling ( )
890
- {
891
- _singleton . _forceEventWaitHandle . Set ( ) ;
892
- }
893
-
894
878
private static void Chord ( ConsoleKeyInfo ? key = null , object arg = null )
895
879
{
896
880
if ( ! key . HasValue )
0 commit comments