From 43357a5198118ef46e2c83a65fc6af33b4100399 Mon Sep 17 00:00:00 2001 From: zerodev1200 <42404360+zerodev1200@users.noreply.github.com> Date: Sun, 20 Oct 2024 03:06:42 +0900 Subject: [PATCH] add handling for SynchronizationContext --- .../ICollectionEventDispatcher.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ObservableCollections/ICollectionEventDispatcher.cs b/src/ObservableCollections/ICollectionEventDispatcher.cs index c2ccfa0..049f580 100644 --- a/src/ObservableCollections/ICollectionEventDispatcher.cs +++ b/src/ObservableCollections/ICollectionEventDispatcher.cs @@ -35,7 +35,16 @@ namespace ObservableCollections public void Post(CollectionEventDispatcherEventArgs ev) { - synchronizationContext.Post(callback, ev); + if (SynchronizationContext.Current == null) + { + // non-UI thread, post the event asynchronously + synchronizationContext.Post(callback, ev); + } + else + { + // UI thread, send the event synchronously + synchronizationContext.Send(callback, ev); + } } static void SendOrPostCallback(object? state)