dotnet / dotnet/maui

PropertyChanged causes COMException (Catastrophic failure) when application is closing

Open
#6,715 9 comments 2 reactions 0 assignees View on GitHub
area-core-dispatching p/2 platform/windows s/triaged s/verified t/bug
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 10h
Merged PRs (30d)
297

Description

### Description

I have a timer that updates a property in a view model. When application is closing this sometimes throws an `COMException`:

System.Runtime.InteropServices.COMException: 'Catastrophic failure (0x8000FFFF (E_UNEXPECTED))'

The more frequent the timer is running, the more likely it is to get this error.

Full sample code: https://github.com/pekspro/MauiIssues/tree/PropertyChangedCausesCOMException

### Steps to Reproduce

1. Create a new MAUI app.
2. Update MainPage.xaml:

``` XAML


```

3. Update MainPage.cs:

``` C#
using System.ComponentModel;

namespace MauiIssues;

public partial class MainPage : ContentPage
{
public ViewModel Model { get; set; } = new ViewModel();

public MainPage()
{
InitializeComponent();
BindingContext = Model;
}
}

public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public int Counter { get; set; }

private System.Timers.Timer Timer = new();

public ViewModel()
{
Timer.Interval = 1;
Timer.Enabled = true;
Timer.Elapsed += (o, s) =>
{
Application.Current.Dispatcher.Dispatch(() =>
{
Counter = (Counter + 1) % 1000;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(Counter)));
});
};
}
}
```

Run the application in Windows, and then close it.

### Version with bug

Release Candidate 2 (current)

### Last version that worked well

Unknown/Other

### Affected platforms

Windows, I was *not* able test on other platforms

### Affected platform versions

Windows 10.0.17763.0

### Did you find any workaround?

_No response_

### Relevant log output

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.