avalonia-threading
Installation
SKILL.md
Avalonia Threading
Version labels per avalonia-versions: (12.0+), (11.x), no label = 11.3/12.0/12.1.
Core Rules
AvaloniaObjects (controls, styled properties) may only be touched from the thread that owns them — normally the UI thread. Violations throwInvalidOperationException: Call from invalid thread.- Avalonia installs an
AvaloniaSynchronizationContexton the UI thread, soawaitin UI-thread code resumes on the UI thread automatically. - Do CPU/IO work off-thread (
Task.Run, async IO); marshal only the result back. - Never block the UI thread (
.Result,.Wait(),.GetAwaiter().GetResult()) — deadlocks and frozen UI. - Multiple UI threads are not supported, even in 12. One dispatcher per thread exists, but apps use one UI thread.
Dispatcher.UIThread
using Avalonia.Threading;
// Fire-and-forget (non-blocking, from any thread)
Dispatcher.UIThread.Post(() => StatusText.Text = "Done");
Dispatcher.UIThread.Post(() => StatusText.Text = "Idle", DispatcherPriority.Background);