Skip to content
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

Proposal: Support Application.ShutdownMode for WinUI 3 #8492

Closed
heftymouse opened this issue May 19, 2023 · 3 comments
Closed

Proposal: Support Application.ShutdownMode for WinUI 3 #8492

heftymouse opened this issue May 19, 2023 · 3 comments
Labels
feature proposal New feature proposal
Milestone

Comments

@heftymouse
Copy link

heftymouse commented May 19, 2023

Proposal: Support Application.ShutdownMode for WinUI 3

Summary

Add the Application.ShutdownMode property from WPF to WinUI 3, for finer-grained control of app lifecycle.

Rationale

A large number of apps are 'daemon-like' and need to run in the background as a system tray icon or otherwise until invoked. However, WinUI 3 in its current state does not allow for this as it terminates the application when all its windows are closed. Adding Application.ShutdownMode would allow developers to configure this and run in the background without resorting to ugly workarounds.

Scope

Capability Priority
This proposal will allow developers to control the automatic termination behaviour of WinUI Must

Important Notes

None

Open Questions

This isn't a perfect solution since apps running without any sort of UI can offer poor UX since the user is unaware that it is still running. In the future, the framework itself may need to support background apps in a user-friendly way (perhaps with microsoft/WindowsAppSDK#713?)

@heftymouse heftymouse added the feature proposal New feature proposal label May 19, 2023
@BreeceW
Copy link
Contributor

BreeceW commented Jan 30, 2024

The Windows App SDK 1.5-experimental2 release from a few days ago has added an experimental API, Application.DispatcherShutdownMode, with the options OnLastWindowClose and OnExplicitShutdown.

@JesseCol JesseCol added this to the WinAppSDK 1.5 milestone Jan 30, 2024
@fredemmott
Copy link

fredemmott commented Feb 26, 2024

For anyone who really needs this now, it's doable by:

  1. getting your hwnd via IWindowNative
  2. subclassing or otherwise hooking the hwnd
  3. intercepting WM_CLOSE and starting your shutdown; call this->Close() (or ShowWindow() on the hwnd)
  4. intercepting WM_DESTROY and ignoring it - the WM_DESTROY message is what triggers the application exit on v1.4
  5. When you actually want to exit, call PostQuitMessage() from the main thread
  6. Leave a TODO/issue/task to remove this when you upgrade to v1.5 :)

My use case is is I have some background coroutines in various threads that must be shutdown cleanly, and I need to keep the dispatcher queues running until that has done; an immediate exit causes problems. The "the app's exiting, it doesn't matter if you just drop everything on the floor" approach doesn't apply when (a) they can connect to external systems (b) improper cleanup can still end up calling the destructors, but in the wrong thread, leading to crashes, Windows Error Reporting popups, etc.

@bpulliam
Copy link
Contributor

This is included in 1.5. See the 1.5 release notes for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature proposal New feature proposal
Projects
None yet
Development

No branches or pull requests

5 participants