-
Notifications
You must be signed in to change notification settings - Fork 698
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: Single instance WinUI desktop apps #4780
Comments
See also microsoft/WindowsAppSDK#111 |
The problem with this is that the main process doesn't know it's window has been activated through a new process. It should be possible but then you can't use the same OnLaunched method and instead a custom one. You can't use SendMessage for example since WinUI doesn't provide a way to let us handle the window messages. |
For those of you who land here looking for how to single-instance your WinUI 3 app, here is what I put together after cobbling together bits and pieces from other posts.
A related task is raising the window of the currently-running instance of your app so that it is on top of other windows. Just calling Window.Activate will not do it, as discussed in #7595 . Here's how I did it. This method must be called on the UI thread.
|
Comment, about the question of which window to bring to the top, this could be addressed simply by adding a virtual OnRedirection method to Application which does nothing and which the app developer can override to brong to top whichever window they desire. |
Proposal: Single instance WinUI desktop apps
Unlike UWP apps which are single instance and for multiple instances need to be enabled manually, Win32 apps are multi instance by default. While this is fine for most apps, some apps particularly need single instancing and today WinUI desktop doesn't provide a way to do that easily.
Summary
Add a property named SingleInstance in the App class. If it's true then new instances of the process will bring the existing window on to the foreground instead of showing a message that existing instance is running like some old desktop apps and not bother to switch and the newly created process should be terminated. One problem here is if the same instance has multiple windows open, you need rightly open the main window and not a secondary window created by the app. A shared mutex could be used to know whether an existing instance is there or not however there's no way to know which window to switch in case of multiple windows unless the developer does it manually themselves as WinUI doesn't know.
Rationale
Scope
Important Notes
I added this code under App's constructor in my app to make it single instance, but it probably won't work with apps that have multiple windows in the same instance.
The text was updated successfully, but these errors were encountered: