-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Change Stop-Process to request Close (instead of Kill) by default #14671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Request Close by default, but allow Force to still Kill directly.
|
I am not certain about some of these lower items on the checklist - this should definitely need some documentation updates to explain that Stop-Process will allow Processes to attempt to close gracefully, but if you need to Force a process to close, then -Force needs to be specified. This may be something warranting a warning/deprecation notice for a version or two, as some people may have been lazily closing without Forcing it when they should have been -Force'ing. |
| // 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(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have no need to have _closeRequest as new field and we could simplify logic, make the code more readable and remove obvious comments - only comments are describing scenarios would be useful.
if (process.HasExited)
{
return;
}
if (Force || !process.CloseMainWindow())
{
process.Kill();
}|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
|
I don't think is consistent with the committee advise on the issue. |
TravisEz13
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think is consistent with the committee advise on the issue.
|
This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 15 days. It will be closed if no further activity occurs within 10 days of this comment. |
|
@TravisEz13 Any advice we can drive #13664 forward? 🙏 |
Request Close by default, but allow Force to still Kill directly.
PR Summary
Change Stop-Process to attempt to CloseMainWindow by default instead of Kill directly.
This should allow the Stop-Process to enable gentler close processes for things like
PR Context
Originally prompted from #13664. When you try to close processes with Kill, they don't have a chance to handle anything, and are immediately closed and then disposed of. In some cases like Tray agents, this even leaves behind blank whole in tray agents and other remnants behind,
CloseMainWindow sends a request for the program to close, instead of forcing it to close immediately. Which gives it a chance to handle closing and prompt for things, save state to be recoverable later, or gracefully close their active connections.
For example, closing a PowerShell ISE Window that has an unopened document gracefully will result in ISE asking to save the document, but killing it will result in immediate close of the program.
My changes adjust so that
Get-Process $ProcessName | Stop-Processwill default to request a graceful Close, and will only Kill the process if the program cannot handle CloseMainWindow or -Force is specified.Some useful test-cases to demonstrate this
Stop-Process -name Notepadand it should prompt to save changes.Stop-Process -name Notepada second time (while the Save prompt is still open) and it will fail to request a gentle close, and will default to Killing the process directly, without saving any changesStop-Process -name winwordand it should prompt to save changes.Stop-Process -name winworda second time (while the Save prompt is still open) and it will still generate a gentle close and word will continue waiting for you to Save/Not Save/CancelStop-Process -name winword -Force(while the Save prompt is open or not) and it will Kill the Process directly, skipping the prompt to Save/Not Save/CancelStop-Process Outlookruns without -Force there is briefly a white icon shown for Outlook, which is labeled "Outlook is Closing"PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.N/Aor can only be tested interactively