@@ -286,15 +286,7 @@ public void Expect(TimeSpan timeout, params ExpectAction[] expectActions)
286286 var returnLength = _encoding . GetByteCount ( returnText ) ;
287287
288288 // Remove processed items from the queue
289- for ( var i = 0 ; i < returnLength && _incoming . Count > 0 ; i ++ )
290- {
291- if ( _expect . Count == _incoming . Count )
292- {
293- _ = _expect . Dequeue ( ) ;
294- }
295-
296- _ = _incoming . Dequeue ( ) ;
297- }
289+ SyncQueuesAndDequeue ( returnLength ) ;
298290
299291 expectAction . Action ( returnText ) ;
300292 expectedFound = true ;
@@ -389,15 +381,7 @@ public string Expect(Regex regex, TimeSpan timeout)
389381 var returnLength = _encoding . GetByteCount ( returnText ) ;
390382
391383 // Remove processed items from the queue
392- for ( var i = 0 ; i < returnLength && _incoming . Count > 0 ; i ++ )
393- {
394- if ( _expect . Count == _incoming . Count )
395- {
396- _ = _expect . Dequeue ( ) ;
397- }
398-
399- _ = _incoming . Dequeue ( ) ;
400- }
384+ SyncQueuesAndDequeue ( returnLength ) ;
401385
402386 break ;
403387 }
@@ -505,15 +489,7 @@ public IAsyncResult BeginExpect(TimeSpan timeout, AsyncCallback callback, object
505489 var returnLength = _encoding . GetByteCount ( returnText ) ;
506490
507491 // Remove processed items from the queue
508- for ( var i = 0 ; i < returnLength && _incoming . Count > 0 ; i ++ )
509- {
510- if ( _expect . Count == _incoming . Count )
511- {
512- _ = _expect . Dequeue ( ) ;
513- }
514-
515- _ = _incoming . Dequeue ( ) ;
516- }
492+ SyncQueuesAndDequeue ( returnLength ) ;
517493
518494 expectAction . Action ( returnText ) ;
519495 callback ? . Invoke ( asyncResult ) ;
@@ -614,15 +590,7 @@ public string ReadLine(TimeSpan timeout)
614590 var bytesProcessed = _encoding . GetByteCount ( text + CrLf ) ;
615591
616592 // remove processed bytes from the queue
617- for ( var i = 0 ; i < bytesProcessed ; i ++ )
618- {
619- if ( _expect . Count == _incoming . Count )
620- {
621- _ = _expect . Dequeue ( ) ;
622- }
623-
624- _ = _incoming . Dequeue ( ) ;
625- }
593+ SyncQueuesAndDequeue ( bytesProcessed ) ;
626594
627595 break ;
628596 }
@@ -687,7 +655,7 @@ public override int Read(byte[] buffer, int offset, int count)
687655 {
688656 for ( ; i < count && _incoming . Count > 0 ; i ++ )
689657 {
690- if ( _expect . Count == _incoming . Count )
658+ if ( _incoming . Count == _expect . Count )
691659 {
692660 _ = _expect . Dequeue ( ) ;
693661 }
@@ -869,5 +837,22 @@ private void OnDataReceived(byte[] data)
869837 {
870838 DataReceived ? . Invoke ( this , new ShellDataEventArgs ( data ) ) ;
871839 }
840+
841+ private void SyncQueuesAndDequeue ( int bytesToDequeue )
842+ {
843+ lock ( _incoming )
844+ {
845+ while ( _incoming . Count > _expect . Count )
846+ {
847+ _ = _incoming . Dequeue ( ) ;
848+ }
849+
850+ for ( var count = 0 ; count < bytesToDequeue && _incoming . Count > 0 ; count ++ )
851+ {
852+ _ = _incoming . Dequeue ( ) ;
853+ _ = _expect . Dequeue ( ) ;
854+ }
855+ }
856+ }
872857 }
873858}
0 commit comments