@@ -24,6 +24,7 @@ public class ConsoleService : IConsoleHost
24
24
{
25
25
#region Fields
26
26
27
+ private bool isReadLoopStarted ;
27
28
private ConsoleReadLine consoleReadLine ;
28
29
private PowerShellContext powerShellContext ;
29
30
@@ -106,21 +107,8 @@ public void StartReadLoop()
106
107
{
107
108
if ( this . EnableConsoleRepl )
108
109
{
109
- if ( this . readLineCancellationToken == null )
110
- {
111
- this . readLineCancellationToken = new CancellationTokenSource ( ) ;
112
-
113
- var terminalThreadTask =
114
- Task . Factory . StartNew (
115
- async ( ) =>
116
- {
117
- await this . StartReplLoop ( this . readLineCancellationToken . Token ) ;
118
- } ) ;
119
- }
120
- else
121
- {
122
- Logger . Write ( LogLevel . Verbose , "StartReadLoop called while read loop is already running" ) ;
123
- }
110
+ this . isReadLoopStarted = true ;
111
+ this . InnerStartReadLoop ( ) ;
124
112
}
125
113
}
126
114
@@ -129,15 +117,8 @@ public void StartReadLoop()
129
117
/// </summary>
130
118
public void CancelReadLoop ( )
131
119
{
132
- if ( this . readLineCancellationToken != null )
133
- {
134
- // Set this to false so that Ctrl+C isn't trapped by any
135
- // lingering ReadKey
136
- Console . TreatControlCAsInput = false ;
137
-
138
- this . readLineCancellationToken . Cancel ( ) ;
139
- this . readLineCancellationToken = null ;
140
- }
120
+ this . isReadLoopStarted = false ;
121
+ this . InnerCancelReadLoop ( ) ;
141
122
}
142
123
143
124
/// <summary>
@@ -148,7 +129,7 @@ public void CancelReadLoop()
148
129
/// <returns>A Task that can be awaited for completion.</returns>
149
130
public async Task ExecuteScriptAtPath ( string scriptPath , string arguments = null )
150
131
{
151
- this . CancelReadLoop ( ) ;
132
+ this . InnerCancelReadLoop ( ) ;
152
133
153
134
// If we don't escape wildcard characters in the script path, the script can
154
135
// fail to execute if say the script name was foo][.ps1.
@@ -161,7 +142,7 @@ await this.powerShellContext.ExecuteScriptString(
161
142
true ,
162
143
false ) ;
163
144
164
- this . StartReadLoop ( ) ;
145
+ this . InnerStartReadLoop ( ) ;
165
146
}
166
147
167
148
/// <summary>
@@ -273,6 +254,41 @@ private void WriteDebuggerBanner(DebuggerStopEventArgs eventArgs)
273
254
}
274
255
}
275
256
257
+ private void InnerStartReadLoop ( )
258
+ {
259
+ if ( this . EnableConsoleRepl )
260
+ {
261
+ if ( this . readLineCancellationToken == null )
262
+ {
263
+ this . readLineCancellationToken = new CancellationTokenSource ( ) ;
264
+
265
+ var terminalThreadTask =
266
+ Task . Factory . StartNew (
267
+ async ( ) =>
268
+ {
269
+ await this . StartReplLoop ( this . readLineCancellationToken . Token ) ;
270
+ } ) ;
271
+ }
272
+ else
273
+ {
274
+ Logger . Write ( LogLevel . Verbose , "InnerStartReadLoop called while read loop is already running" ) ;
275
+ }
276
+ }
277
+ }
278
+
279
+ private void InnerCancelReadLoop ( )
280
+ {
281
+ if ( this . readLineCancellationToken != null )
282
+ {
283
+ // Set this to false so that Ctrl+C isn't trapped by any
284
+ // lingering ReadKey
285
+ Console . TreatControlCAsInput = false ;
286
+
287
+ this . readLineCancellationToken . Cancel ( ) ;
288
+ this . readLineCancellationToken = null ;
289
+ }
290
+ }
291
+
276
292
private async Task StartReplLoop ( CancellationToken cancellationToken )
277
293
{
278
294
do
@@ -431,20 +447,20 @@ private void activePromptHandler_PromptCancelled(object sender, EventArgs e)
431
447
private void PowerShellContext_DebuggerStop ( object sender , System . Management . Automation . DebuggerStopEventArgs e )
432
448
{
433
449
// Cancel any existing prompt first
434
- this . CancelReadLoop ( ) ;
450
+ this . InnerCancelReadLoop ( ) ;
435
451
436
452
this . WriteDebuggerBanner ( e ) ;
437
- this . StartReadLoop ( ) ;
453
+ this . InnerStartReadLoop ( ) ;
438
454
}
439
455
440
456
private void PowerShellContext_DebuggerResumed ( object sender , System . Management . Automation . DebuggerResumeAction e )
441
457
{
442
- this . CancelReadLoop ( ) ;
458
+ this . InnerCancelReadLoop ( ) ;
443
459
}
444
460
445
461
private void PowerShellContext_ExecutionStatusChanged ( object sender , ExecutionStatusChangedEventArgs eventArgs )
446
462
{
447
- if ( this . EnableConsoleRepl )
463
+ if ( this . EnableConsoleRepl && this . isReadLoopStarted )
448
464
{
449
465
if ( eventArgs . ExecutionStatus == ExecutionStatus . Aborted )
450
466
{
@@ -464,12 +480,12 @@ private void PowerShellContext_ExecutionStatusChanged(object sender, ExecutionSt
464
480
if ( eventArgs . ExecutionStatus != ExecutionStatus . Running )
465
481
{
466
482
// Execution has completed, start the input prompt
467
- this . StartReadLoop ( ) ;
483
+ this . InnerStartReadLoop ( ) ;
468
484
}
469
485
else
470
486
{
471
487
// A new command was started, cancel the input prompt
472
- this . CancelReadLoop ( ) ;
488
+ this . InnerCancelReadLoop ( ) ;
473
489
this . WriteOutput ( string . Empty ) ;
474
490
}
475
491
}
@@ -478,9 +494,9 @@ private void PowerShellContext_ExecutionStatusChanged(object sender, ExecutionSt
478
494
( eventArgs . ExecutionStatus == ExecutionStatus . Failed ||
479
495
eventArgs . HadErrors ) )
480
496
{
481
- this . CancelReadLoop ( ) ;
497
+ this . InnerCancelReadLoop ( ) ;
482
498
this . WriteOutput ( string . Empty ) ;
483
- this . StartReadLoop ( ) ;
499
+ this . InnerStartReadLoop ( ) ;
484
500
}
485
501
}
486
502
}
0 commit comments