@@ -422,7 +422,7 @@ internal override void Prepare(IDictionary psDefaultParameterValues)
422
422
catch ( Exception )
423
423
{
424
424
// Do cleanup in case of exception
425
- CleanUp ( ) ;
425
+ CleanUp ( killBackgroundProcess : true ) ;
426
426
throw ;
427
427
}
428
428
}
@@ -444,7 +444,7 @@ internal override void ProcessRecord()
444
444
catch ( Exception )
445
445
{
446
446
// Do cleanup in case of exception
447
- CleanUp ( ) ;
447
+ CleanUp ( killBackgroundProcess : true ) ;
448
448
throw ;
449
449
}
450
450
}
@@ -904,7 +904,7 @@ internal override void Complete()
904
904
finally
905
905
{
906
906
// Do some cleanup
907
- CleanUp ( ) ;
907
+ CleanUp ( killBackgroundProcess : false ) ;
908
908
}
909
909
910
910
// An exception was thrown while attempting to run the program
@@ -1161,7 +1161,8 @@ internal void StopProcessing()
1161
1161
/// <summary>
1162
1162
/// Aggressively clean everything up...
1163
1163
/// </summary>
1164
- private void CleanUp ( )
1164
+ /// <param name="killBackgroundProcess">If set, also terminate background process.</param>
1165
+ private void CleanUp ( bool killBackgroundProcess )
1165
1166
{
1166
1167
// We need to call 'NotifyEndApplication' as appropriate during cleanup
1167
1168
if ( _hasNotifiedBeginApplication )
@@ -1171,17 +1172,20 @@ private void CleanUp()
1171
1172
1172
1173
try
1173
1174
{
1174
- // on Unix, we need to kill the process to ensure it terminates as Dispose() merely
1175
- // closes the redirected streams and the processs does not exit on macOS. However,
1176
- // on Windows, a winexe like notepad should continue running so we don't want to kill it.
1175
+ // on Unix, we need to kill the process (if not running in background) to ensure it terminates,
1176
+ // as Dispose() merely closes the redirected streams and the process does not exit.
1177
+ // However, on Windows, a winexe like notepad should continue running so we don't want to kill it.
1177
1178
#if UNIX
1178
- try
1179
- {
1180
- _nativeProcess ? . Kill ( ) ;
1181
- }
1182
- catch
1179
+ if ( killBackgroundProcess || ! _isRunningInBackground )
1183
1180
{
1184
- // Ignore all exception since it is cleanup.
1181
+ try
1182
+ {
1183
+ _nativeProcess ? . Kill ( ) ;
1184
+ }
1185
+ catch
1186
+ {
1187
+ // Ignore all exceptions since it is cleanup.
1188
+ }
1185
1189
}
1186
1190
#endif
1187
1191
_nativeProcess ? . Dispose ( ) ;
0 commit comments