From 78d00be0fbc3e7777c4290129973c17ff5470ead Mon Sep 17 00:00:00 2001 From: PsychoData Date: Tue, 26 Jan 2021 17:04:51 -0500 Subject: [PATCH] Change Stop-Process to request Close by default Request Close by default, but allow Force to still Kill directly. --- .../commands/management/Process.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index 3451fd96ac2..437ecea406c 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -1247,7 +1247,16 @@ protected override void ProcessRecord() StopDependentService(process); } - if (!process.HasExited) + // If the process has not exited and we are not Forcing, attempt close request + if (!process.HasExited && !Force) + { + _closeRequest = process.CloseMainWindow(); + } + + // If CloseMainWindow request was not successful, or unsupported, fallback to Kill + // In some cases, this can have the affect that running Stop-Process without Force + /// twice would revert to Kill() + if (!_closeRequest) { process.Kill(); } @@ -1297,6 +1306,11 @@ protected override void EndProcessing() /// private bool _shouldKillCurrentProcess; + /// + /// Was the current process successfully sent a Close Request, or should it be Killed. + /// + private bool _closeRequest; + /// /// Boolean variables to display the warning using ShouldContinue. ///