@@ -17,9 +17,16 @@ internal LongRunningApplicationSubscription(ObservableProcess process, Composite
1717
1818 private IDisposable Subscription { get ; }
1919
20- public ObservableProcess Process { get ; }
20+ private ObservableProcess Process { get ; }
2121
22+ public bool Running { get ; internal set ; }
23+
24+ internal ManualResetEvent WaitHandle { get ; } = new ( false ) ;
25+
26+ /// <inheritdoc cref="ObservableProcessBase{TConsoleOut}.SendControlC(int)"/>>
2227 public bool SendControlC ( int processId ) => Process . SendControlC ( processId ) ;
28+
29+ /// <inheritdoc cref="ObservableProcessBase{TConsoleOut}.SendControlC()"/>>
2330 public void SendControlC ( ) => Process . SendControlC ( ) ;
2431
2532 public void Dispose ( )
@@ -52,48 +59,50 @@ public static partial class Proc
5259 /// <returns>The exit code and whether the process completed</returns>
5360 public static LongRunningApplicationSubscription StartLongRunning ( LongRunningArguments arguments , TimeSpan waitForStartedConfirmation , IConsoleOutWriter consoleOutWriter = null )
5461 {
55- var started = false ;
56- var confirmWaitHandle = new ManualResetEvent ( false ) ;
5762 var composite = new CompositeDisposable ( ) ;
5863 var process = new ObservableProcess ( arguments ) ;
64+ var subscription = new LongRunningApplicationSubscription ( process , composite ) ;
5965 consoleOutWriter ??= new ConsoleOutColorWriter ( ) ;
6066
6167 var startedConfirmation = arguments . StartedConfirmationHandler ?? ( _ => true ) ;
6268
6369 if ( arguments . StartedConfirmationHandler != null && arguments . StopBufferingAfterStarted )
64- arguments . KeepBufferingLines = _ => ! started ;
70+ arguments . KeepBufferingLines = _ => ! subscription . Running ;
6571
6672 Exception seenException = null ;
6773 composite . Add ( process ) ;
6874 composite . Add ( process . SubscribeLinesAndCharacters (
6975 l =>
7076 {
71- if ( startedConfirmation ( l ) )
72- {
73- confirmWaitHandle . Set ( ) ;
74- started = true ;
75- }
77+ if ( ! startedConfirmation ( l ) ) return ;
78+ subscription . Running = true ;
79+ subscription . WaitHandle . Set ( ) ;
7680 } ,
7781 e =>
7882 {
7983 seenException = e ;
80- confirmWaitHandle . Set ( ) ;
84+ subscription . Running = false ;
85+ subscription . WaitHandle . Set ( ) ;
8186 } ,
8287 l => consoleOutWriter . Write ( l ) ,
83- l => consoleOutWriter . Write ( l )
84- )
88+ l => consoleOutWriter . Write ( l ) ,
89+ onCompleted : ( ) =>
90+ {
91+ subscription . Running = false ;
92+ subscription . WaitHandle . Set ( ) ;
93+ } )
8594 ) ;
8695
8796 if ( seenException != null ) ExceptionDispatchInfo . Capture ( seenException ) . Throw ( ) ;
8897 if ( arguments . StartedConfirmationHandler == null )
8998 {
90- confirmWaitHandle . Set ( ) ;
91- started = true ;
99+ subscription . Running = true ;
100+ subscription . WaitHandle . Set ( ) ;
92101 }
93102 else
94103 {
95- var completed = confirmWaitHandle . WaitOne ( waitForStartedConfirmation ) ;
96- if ( completed ) return new ( process , composite ) ;
104+ var completed = subscription . WaitHandle . WaitOne ( waitForStartedConfirmation ) ;
105+ if ( completed ) return subscription ;
97106 var pwd = arguments . WorkingDirectory ;
98107 var args = arguments . Args . NaivelyQuoteArguments ( ) ;
99108 var printBinary = arguments . OnlyPrintBinaryInExceptionMessage
@@ -102,7 +111,7 @@ public static LongRunningApplicationSubscription StartLongRunning(LongRunningArg
102111 throw new ProcExecException ( $ "Could not yield started confirmation after { waitForStartedConfirmation } while running { printBinary } ") ;
103112 }
104113
105- return new ( process , composite ) ;
114+ return subscription ;
106115 }
107116 }
108117}
0 commit comments