@@ -326,7 +326,7 @@ internal override void Prepare(IDictionary psDefaultParameterValues)
326
326
catch ( Exception )
327
327
{
328
328
// Do cleanup in case of exception
329
- CleanUp ( ) ;
329
+ CleanUp ( killBackgroundProcess : true ) ;
330
330
throw ;
331
331
}
332
332
}
@@ -348,7 +348,7 @@ internal override void ProcessRecord()
348
348
catch ( Exception )
349
349
{
350
350
// Do cleanup in case of exception
351
- CleanUp ( ) ;
351
+ CleanUp ( killBackgroundProcess : true ) ;
352
352
throw ;
353
353
}
354
354
}
@@ -782,7 +782,7 @@ internal override void Complete()
782
782
finally
783
783
{
784
784
// Do some cleanup
785
- CleanUp ( ) ;
785
+ CleanUp ( killBackgroundProcess : false ) ;
786
786
}
787
787
788
788
// An exception was thrown while attempting to run the program
@@ -1060,7 +1060,8 @@ internal void StopProcessing()
1060
1060
/// <summary>
1061
1061
/// Aggressively clean everything up...
1062
1062
/// </summary>
1063
- private void CleanUp ( )
1063
+ /// <param name="killBackgroundProcess">If set, also terminate background process.</param>
1064
+ private void CleanUp ( bool killBackgroundProcess )
1064
1065
{
1065
1066
// We need to call 'NotifyEndApplication' as appropriate during cleanup
1066
1067
if ( _hasNotifiedBeginApplication )
@@ -1070,23 +1071,23 @@ private void CleanUp()
1070
1071
1071
1072
try
1072
1073
{
1073
- if ( _nativeProcess != null )
1074
- {
1075
- // on Unix, we need to kill the process to ensure it terminates as Dispose() merely
1076
- // closes the redirected streams and the processs does not exit on macOS. However,
1077
- // on Windows, a winexe like notepad should continue running so we don't want to kill it.
1074
+ // on Unix, we need to kill the process (if not running in background) to ensure it terminates,
1075
+ // as Dispose() merely closes the redirected streams and the process does not exit.
1076
+ // However, on Windows, a winexe like notepad should continue running so we don't want to kill it.
1078
1077
#if UNIX
1078
+ if ( killBackgroundProcess || ! _isRunningInBackground )
1079
+ {
1079
1080
try
1080
1081
{
1081
- _nativeProcess . Kill ( ) ;
1082
+ _nativeProcess ? . Kill ( ) ;
1082
1083
}
1083
1084
catch
1084
1085
{
1085
- // Ignore all exception since it is cleanup.
1086
+ // Ignore all exceptions since it is cleanup.
1086
1087
}
1087
- #endif
1088
- _nativeProcess . Dispose ( ) ;
1089
1088
}
1089
+ #endif
1090
+ _nativeProcess . Dispose ( ) ;
1090
1091
}
1091
1092
catch ( Exception )
1092
1093
{
0 commit comments