dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.99k stars 1.72k forks source link

[Windows, 9.0 Preview 7] Every quit and exit method throws an exception #24486

Open janne-hmp opened 2 weeks ago

janne-hmp commented 2 weeks ago

Description

I've been trying to exit my .NET MAUI 9.0 Windows application cleanly, but it seems that every exit or quit method throws an exception (at least so in .NET MAUI 9.0 Preview 7). The exceptions are different in each case, though. I tried with and without MainThread.BeginInvokeOnMainThread in the case this were a threading issue, but the results are the same either way.

Microsoft.Maui.Controls.Application.Exit():

image

Application.Current.Quit():

image

Environment.Exit(0):

image

What is the appropriate way to close a MAUI Windows app when the user wants to close it, e.g., by pressing a Quit button on the app's UI?

Steps to Reproduce

  1. Build and start GnollHackM for Windows in Debug mode
  2. Press Exit on the MainPage.
  3. Any of the aforementioned exit or quit calls throws an exception

Link to public reproduction project repository

https://github.com/hyvanmielenpelit/GnollHack

Version with bug

9.0.0-preview.7.24407.4

Is this a regression from previous behavior?

No

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

Windows 10.0.22631

Did you find any workaround?

No, but the exception crashes the app, so it sort of does the trick, but SentrySDK reports the crash so we get a huge number of crash exceptions reported.

Relevant log output

No response

github-actions[bot] commented 2 weeks ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

janne-hmp commented 2 weeks ago

Interestingly, this seems to happen only when the app is in full screen mode. The exception is not thrown in WindowedMode. Full screen mode is implemented via:

                        bool maximizeWindow = !Preferences.Get("WindowedMode", false);
                        if(maximizeWindow)
                        {
                            window.ExtendsContentIntoTitleBar = false;
                            switch (appWindow.Presenter)
                            {
                                case Microsoft.UI.Windowing.OverlappedPresenter overlappedPresenter:
                                    overlappedPresenter.SetBorderAndTitleBar(false, false);
                                    overlappedPresenter.Maximize();
                                    break;
                            }
                        }

I did also test this with default Maui app in full screen mode, and at least Environment.Exit(0) does not work in full screen there, similar to my app. However, by contrast, Application.Current.Quit seems to work without exception, so there may be more than just the full screen mode in my app's setup that causes the crash.