Skip to content

Conversation

@PsychoData
Copy link

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

  • Tray Agents
  • Office Documents
  • Notepad windows
  • Any program/window which has a state that prefers to gracefully close itself when possible

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.

image

My changes adjust so that Get-Process $ProcessName | Stop-Process will 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

  • Notepad Prompts to close if you run a gentle close Stop-Process once, but falls back to kill on trying to run a second time, because the "Do you want to Save?" window is Modal
    • Open Notepad and enter some text but do not save.
    • Run Stop-Process -name Notepad and it should prompt to save changes.
    • Do not close the prompt to save
    • Run Stop-Process -name Notepad a 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 changes
  • Microsoft Word handles the close method and prompts to save with a custom window that is not Modal and will keep accepting gentle close Stop-Process requests without forcing it to close.
    • Open Microsoft Word and enter some text but do not save
    • Run Stop-Process -name winword and it should prompt to save changes.
    • Do not close the prompt to save
    • Run Stop-Process -name winword a 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/Cancel
    • Run Stop-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/Cancel
  • When Microsoft Outlook is sent a Close signal, it will attempt to close sessions and finish sending any items in the outbox before closing itself, but with a Kill signal it closes immediately, without exiting gracefully
    • Note in the animation below, when Stop-Process Outlook runs without -Force there is briefly a white icon shown for Outlook, which is labeled "Outlook is Closing"
    • But when Closed with a KILL it immediately stops

image

PR Checklist

Request Close by default, but allow Force to still Kill directly.
@PsychoData
Copy link
Author

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.

Comment on lines +1250 to 1262
// 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();
}
Copy link
Collaborator

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();
}

@ghost ghost added the Review - Needed The PR is being reviewed label Feb 4, 2021
@ghost
Copy link

ghost commented Feb 4, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@TravisEz13
Copy link
Member

I don't think is consistent with the committee advise on the issue.

@ghost ghost removed the Review - Needed The PR is being reviewed label Feb 18, 2021
Copy link
Member

@TravisEz13 TravisEz13 left a 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.

@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Feb 18, 2021
@ghost ghost added the Stale label Mar 5, 2021
@ghost
Copy link

ghost commented Mar 5, 2021

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.

@ghost ghost closed this Mar 16, 2021
@kapsiR
Copy link

kapsiR commented Dec 5, 2024

@TravisEz13 Any advice we can drive #13664 forward? 🙏

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants