@@ -46,7 +46,7 @@ public class Launcher
4646 private LaunchOptions _options ;
4747 private TaskCompletionSource < bool > _waitForChromeToClose ;
4848 private static int _processCount = 0 ;
49-
49+ private bool _processLoaded ;
5050 private const string UserDataDirArgument = "--user-data-dir" ;
5151 #endregion
5252
@@ -153,6 +153,7 @@ public async Task<Browser> LaunchAsync(LaunchOptions options, int chromiumRevisi
153153 var keepAliveInterval = options . KeepAliveInterval ;
154154
155155 _connection = await Connection . Create ( browserWSEndpoint , connectionDelay , keepAliveInterval ) ;
156+ _processLoaded = true ;
156157
157158 if ( options . LogProcess )
158159 {
@@ -164,11 +165,11 @@ public async Task<Browser> LaunchAsync(LaunchOptions options, int chromiumRevisi
164165 catch ( Exception ex )
165166 {
166167 ForceKillChrome ( ) ;
167- throw new Exception ( "Failed to create connection" , ex ) ;
168+ throw new ChromeProcessException ( "Failed to create connection" , ex ) ;
168169 }
169170
170171 }
171-
172+
172173 public async Task TryDeleteUserDataDir ( int times = 10 , TimeSpan ? delay = null )
173174 {
174175 if ( ! IsChromeClosed )
@@ -225,7 +226,7 @@ public static string GetTemporaryDirectory()
225226 #endregion
226227
227228 #region Private methods
228-
229+
229230 private Task < string > WaitForEndpoint ( Process chromeProcess , int timeout , bool dumpio )
230231 {
231232 var taskWrapper = new TaskCompletionSource < string > ( ) ;
@@ -236,10 +237,14 @@ private Task<string> WaitForEndpoint(Process chromeProcess, int timeout, bool du
236237
237238 EventHandler exitedEvent = ( sender , e ) =>
238239 {
240+ if ( _options . LogProcess && ! _processLoaded )
241+ {
242+ Console . WriteLine ( $ "PROCESS COUNT: { Interlocked . Increment ( ref _processCount ) } ") ;
243+ }
244+
239245 CleanUp ( ) ;
240246
241- var error = chromeProcess . StandardError . ReadToEnd ( ) ;
242- taskWrapper . SetException ( new ChromeProcessException ( $ "Failed to launch chrome! { error } ") ) ;
247+ taskWrapper . SetException ( new ChromeProcessException ( $ "Failed to launch chrome! { output } ") ) ;
243248 } ;
244249
245250 chromeProcess . ErrorDataReceived += ( sender , e ) =>
@@ -340,7 +345,7 @@ private void ForceKillChrome()
340345 {
341346 try
342347 {
343- if ( _chromeProcess . Id != 0 && Process . GetProcessById ( _chromeProcess . Id ) != null )
348+ if ( _chromeProcess . Id != 0 && ! _chromeProcess . HasExited && Process . GetProcessById ( _chromeProcess . Id ) != null )
344349 {
345350 _chromeProcess . Kill ( ) ;
346351 _chromeProcess . WaitForExit ( ) ;
0 commit comments