@@ -140,10 +140,7 @@ internal void ReleaseRef()
140140 }
141141 }
142142 }
143- if ( pws != null )
144- {
145- pws . Dispose ( ) ;
146- }
143+ pws ? . Dispose ( ) ;
147144 }
148145
149146 /// <summary>
@@ -202,10 +199,7 @@ private void SetExited()
202199
203200 _exited = true ;
204201 _exitTime = DateTime . Now ;
205- if ( _exitedEvent != null )
206- {
207- _exitedEvent . Set ( ) ;
208- }
202+ _exitedEvent ? . Set ( ) ;
209203 }
210204
211205 /// <summary>Ensures an exited event has been initialized and returns it.</summary>
@@ -447,10 +441,7 @@ internal bool WaitForExit(int millisecondsTimeout)
447441 // be caught first thing in the loop where we check _exited, and if it didn't exit,
448442 // our remaining time will be zero, so we'll do a quick remaining check and bail.
449443 waitTask . Wait ( ) ;
450- if ( cts != null )
451- {
452- cts . Dispose ( ) ;
453- }
444+ cts ? . Dispose ( ) ;
454445 }
455446 else
456447 {
@@ -476,6 +467,12 @@ internal bool WaitForExit(int millisecondsTimeout)
476467
477468 return _waitInProgress = Task . Run ( async delegate // Task.Run used because of potential blocking in CheckForExit
478469 {
470+ // Arbitrary values chosen to balance delays with polling overhead. Start with fast polling
471+ // to handle quickly completing processes, but fall back to longer polling to minimize
472+ // overhead for those that take longer to complete.
473+ const int StartingPollingIntervalMs = 1 , MaxPollingIntervalMs = 100 ;
474+ int pollingIntervalMs = StartingPollingIntervalMs ;
475+
479476 try
480477 {
481478 // While we're not canceled
@@ -497,8 +494,8 @@ internal bool WaitForExit(int millisecondsTimeout)
497494 // Wait
498495 try
499496 {
500- const int PollingIntervalMs = 100 ; // arbitrary value chosen to balance delays with polling overhead
501- await Task . Delay ( PollingIntervalMs , cancellationToken ) ;
497+ await Task . Delay ( pollingIntervalMs , cancellationToken ) ;
498+ pollingIntervalMs = Math . Min ( pollingIntervalMs * 2 , MaxPollingIntervalMs ) ;
502499 }
503500 catch ( OperationCanceledException ) { }
504501 }
0 commit comments