Merge pull request #83 from zerodev1200/fix-synccontext-thread-handling

Improve synchronization handling in event dispatcher
This commit is contained in:
Yoshifumi Kawai 2024-10-21 11:55:46 +09:00 committed by GitHub
commit a4d80bda85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -35,7 +35,16 @@ namespace ObservableCollections
public void Post(CollectionEventDispatcherEventArgs ev) 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) static void SendOrPostCallback(object? state)