dotnet / dotnet/maui

Some Essentials APIs crash when using multi-window

Open
#5,536 5 comments 2 reactions 0 assignees View on GitHub
area-essentials legacy-area-desktop p/2 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

Most of the sensors (but some others) are using an incorrect way of marshalling the events to the main thread. They pick the active window, but this is incorrect because if you have multiple windows and then try update another window, there is a marshalling error.

### Steps to Reproduce

1. Create a new window on the default thread
2. Create a new window on a new thread
3. Start listening to accelerometer on both windows
4. Update a label on each window in the event
5. One window (the non-active window) will throw an exception

Start a new window in a new thread like this:

```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_

Contributor guide

Open the contributing guide

Research direction

Start by tracing how Essentials APIs, especially the accelerometer, marshal events to the main thread in the multi-window scenario described. Reproduce the two-window setup using Application.Current.OpenWindow and verify that both windows can update their labels from sensor events without a marshalling exception.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.