dotnet / dotnet/maui

Maui crashes when launching a new window in a new thread

Open
#5,534 4 comments 0 reactions 0 assignees View on GitHub
area-controls-window blocked external legacy-area-desktop partner/winui platform/windows s/triaged t/bug t/desktop
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 14h
Merged PRs (30d)
296

Description

### Description

Starting a new thread and running:

```cs
Application.Current.OpenWindow(new Window(new MultiWindowPage()));
```

crashes the app. Something about the templates for the `WindowRootView` not applying.

However, this works:

```cs
new Microsoft.UI.Xaml.Window().Activate();
```

Seems to be our code not WinUI.

### Steps to Reproduce

```cs
var thread = new Thread(() =>
{
Microsoft.UI.Xaml.Application.Start(async (p) =>
{
var dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();
var syncContext = new Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext(dispatcherQueue);
SynchronizationContext.SetSynchronizationContext(syncContext);

Application.Current.OpenWindow(new Window(new MultiWindowPage()));
});
});

thread.SetApartmentState(ApartmentState.STA);
thread.Start();
```

### Version with bug

Preview 14 (current)

### Last version that worked well

Unknown/Other

### Affected platforms

Windows

### Affected platform versions

All

### Did you find any workaround?

_No response_

### Relevant log output

_No response_

> VS bug [#1786427](https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1786427)

Contributor guide

Open the contributing guide

Research direction

Start by running the Windows reproduction with Application.Current.OpenWindow(new Window(new MultiWindowPage())) from the provided STA thread and compare it with direct Microsoft.UI.Xaml.Window activation. Trace the WindowRootView template application on the new thread. Done means MAUI opens the new window without crashing in this scenario and existing behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.