Skip to content

In .NET 9 atempting to show a ContentDialog at the Window.Closed event handler generates a Win32 Exception #10519

@gmgallo

Description

@gmgallo

Describe the bug

I'm updating an application from NET 6 to NET 9. I have a handler for the Window.Close event to ask user confirmation and do cleanup if OK to close.
I'm making sure that the dialog opens on the UI thread. I can't catch the Win32 exception in code and the VS 2022 debugger can't either even with all Win32 exceptions checked. It follows some code snippets:

    public Page1ViewModel()
    {
        dispatcherQueue = DispatcherQueue.GetForCurrentThread();
        App.MainWindow.Closed += VmMainWindow_Closed;

       // more init code
    }

    private async void VmMainWindow_Closed(object sender, WindowEventArgs args)
    {
        try
        {
             var answer = await ShowDialogFromNonUIThreadAsync();

            if (answer != ContentDialogResult.Primary)  // if user didn't OK do not close
            {
                 args.Handled = true;
                return;
            }
        }
        catch (Exception ex) 
        {
            AppLogger?.LogException(ex);
        }
       args.Handled = false;
    }                                                                        

   DispatcherQueue? dispatcherQueue
   {
       get =>_dispatcherQueue;
       set
       {
           if (value != null)
           {
               _dispatcherQueue = value;
           }
       }
   }
   DispatcherQueue _dispatcherQueue = null;

// Show a ContentDialog and wait for the result from a non-UI thread
public async Task<ContentDialogResult> ShowDialogFromNonUIThreadAsync()
{
    // Create a TaskCompletionSource to wait for the result
    var tcs = new TaskCompletionSource<ContentDialogResult>();

    if (dispatcherQueue != null) // make sure we captured the UI thread DispatcherQueue
    {
        // Enqueue the dialog creation and showing on the UI thread
        dispatcherQueue.TryEnqueue(async () =>
        {
            try
            {
                var dialog = new ContentDialog
                {
                    Title = "Window Closing",
                    Content = "OK to close the App?",
                    PrimaryButtonText = "OK",
                    CloseButtonText = "Cancel"
                };

                // Show the dialog and set the result in the TaskCompletionSource

                **// THE NEXT LINE THROWS AN EXCEPTION IN .NET 9
                ContentDialogResult result = await dialog.ShowAsync();**

                tcs.SetResult(result); // Set the result
            }
            catch (Win32Exception ex)
            {
                AppLogger?.LogException(ex);
            }
            catch (COMException ex)
            {
                AppLogger?.LogException(ex);
            }
        });
    }
    else
    {
        tcs.SetException(new InvalidOperationException("DispatcherQueue is not available."));
    }

    // Wait for the dialog result
    return await tcs.Task;
}

Steps to reproduce the bug

Create a simple WinUI 3 app and reproduce the code above

Expected behavior

No response

Screenshots

No response

NuGet package version

None

Windows version

Windows 10 (21H2): Build 19044

Additional context

Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.13.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions