Logo van Avalonia Threading

Avalonia Threading

Community
linuxdevel
avalonia-threading

Use when code touches Avalonia threading — updating UI from background threads, Dispatcher.UIThread Post/InvokeAsync/Invoke, CheckAccess/VerifyAccess ("Call from invalid thread"), DispatcherPriority, async/await in ViewModels, ConfigureAwait, DispatcherTimer, progress reporting, cancellation, ObservableCollection updates off the UI thread, AvaloniaSynchronizationContext, or the Avalonia 12 multi-dispatcher changes (Dispatcher.CurrentDispatcher, Dispatcher.Yield).

Overzicht

Uitgeverlinuxdevel
RepositoryAvalonia-skills
Skillnaamavalonia-threading
Sterren
50
Forks
4
Meegeleverde bestanden
Alleen instructies
LicentieMIT
Links
  • Instructies in Markdown

    Een SKILL.md-bestand dat het model laadt wanneer nodig, zodat het alleen tokens kost als een verzoek er daadwerkelijk bij past.

  • Werkt met elk LLM

    AI-skills zijn gewone Markdown, geen aanbiederspecifieke code. Daardoor werken ze met GPT, Claude, Gemini, Grok of een lokaal model.

  • Compleet zonder extra bestanden

    Alles wat het model nodig heeft, staat in de instructies — geen extra bestanden om te synchroniseren.

  • Open source

    Gepubliceerd door linuxdevel op GitHub. Lees de broninhoud voordat je de skill installeert.

Installatie

Installeer de AI-skill Avalonia Threading in TypingMind om deze met elke LLM te gebruiken, of voeg de skill toe aan een andere agent die SKILL.md kan lezen.

1

Installeren in TypingMind

TypingMind installeert een skill rechtstreeks vanuit de GitHub-map — het leest SKILL.md, bundelt de bijbehorende bestanden en slaat het resultaat lokaal op.

  1. Open de app en ga naar Plug-ins → Skills.
  2. Kies "Installeren via GitHub".
  3. Plak de onderstaande URL van de skillmap en bevestig.
    https://github.com/linuxdevel/Avalonia-skills/tree/main/skills/avalonia/avalonia-threading
  4. Schakel de skill in elke chat in waarin je deze beschikbaar wilt hebben.
Plug-ins → Skills → Nieuwe skill → Via GitHub-URL. Plak vervolgens de map-URL en druk op Doorgaan.
2

Installeren in een andere agent

Elke agent die het Agent Skills-formaat kan lezen, kan deze skill gebruiken — kopieer de map naar de skillmap van die agent.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/linuxdevel/Avalonia-skills.git /tmp/Avalonia-skills
mkdir -p .claude/skills
cp -r /tmp/Avalonia-skills/skills/avalonia/avalonia-threading .claude/skills/avalonia-threading
Start Claude Code na het kopiëren opnieuw op, zodat de nieuwe skill wordt herkend.

Gebruik deze skill in TypingMind

Schakel Avalonia Threading in een willekeurige chat in TypingMind in en het model doet de rest. De naam en beschrijving staan in de systeemprompt. Zodra een verzoek erbij past, laadt het model zelf de volledige instructies — je hoeft de skill nooit handmatig aan te roepen en deze kost pas tokens wanneer je de skill daadwerkelijk gebruikt.

Het model laadt Avalonia Threading automatisch zodra een verzoek erbij past.

Werkt met elk AI-model

AI-skills zijn gewone Markdown-instructies in plaats van aanbiederspecifieke code. Daardoor is Avalonia Threading niet gebonden aan het model waarvoor de skill is geschreven. Installeer de skill één keer in TypingMind en gebruik deze met GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama of een lokaal model dat je zelf draait — allemaal met je eigen API-sleutels.

  • Alleen geladen wanneer nodig

    De systeemprompt bevat alleen de naam en beschrijving. De instructies worden bij het eerste bijpassende verzoek opgehaald, dus een ongebruikte skill kost niets.

  • Wissel van model tijdens een chat

    Omdat de skill uit instructies bestaat en niet uit code, blijft deze werken als je van model wisselt — het volgende model leest hetzelfde SKILL.md-bestand.

Skillinstructies

Dit is de inhoud van SKILL.md die het model laadt. Lees deze vóór de installatie — een skill bestaat uit instructies die je model zal volgen.

Avalonia Threading

Version labels per avalonia-versions: (12.0+), (11.x), no label = 11.3/12.0/12.1.

Core Rules

  1. AvaloniaObjects (controls, styled properties) may only be touched from the thread that owns them — normally the UI thread. Violations throw InvalidOperationException: Call from invalid thread.
  2. Avalonia installs an AvaloniaSynchronizationContext on the UI thread, so await in UI-thread code resumes on the UI thread automatically.
  3. Do CPU/IO work off-thread (Task.Run, async IO); marshal only the result back.
  4. Never block the UI thread (.Result, .Wait(), .GetAwaiter().GetResult()) — deadlocks and frozen UI.
  5. Multiple UI threads are not supported, even in 12. One dispatcher per thread exists, but apps use one UI thread.

Dispatcher.UIThread

csharp
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);

// Await completion / get a value back
await Dispatcher.UIThread.InvokeAsync(() => StatusText.Text = "Done");
var text = await Dispatcher.UIThread.InvokeAsync(() => SearchBox.Text);

// Async callback overload (returns Task, awaits inner work)
await Dispatcher.UIThread.InvokeAsync(async () => { await LoadAsync(); });

// Blocking — avoid from UI-bound flows; fine for rare sync handoffs from a worker
Dispatcher.UIThread.Invoke(() => StatusText.Text = "Done");

// Access checks
if (Dispatcher.UIThread.CheckAccess()) { /* on UI thread */ }
Dispatcher.UIThread.VerifyAccess();      // throws if not on UI thread
MemberReturnsDefault priorityUse when
Post(Action, priority = default)voidDefaultFire-and-forget; safest, cannot deadlock
Post(SendOrPostCallback, object? arg, priority = default)voidDefaultAvoid closure allocation
InvokeAsync(Action [, priority [, ct]])DispatcherOperationDefaultNeed to await / abort / cancel
InvokeAsync<T>(Func<T> [, priority [, ct]])DispatcherOperation<T>DefaultRead a value from UI thread
InvokeAsync(Func<Task> [, priority])TaskDefaultAsync lambda on UI thread
InvokeAsync<T>(Func<Task<T>> [, priority])Task<T>DefaultAsync lambda with result
Invoke(Action) / Invoke<T>(Func<T>) (+ priority, ct, timeout overloads)void / TSendSynchronous; blocks caller
CheckAccess()bool—Branch on current thread
VerifyAccess()void—Assert in debug paths
ToTaskScheduler() / ToTaskScheduler(priority)TaskScheduler—Run TPL tasks on the dispatcher

DispatcherOperation members: Status, Priority (settable), Abort(), Wait(), Wait(TimeSpan), GetTask(), GetAwaiter(), events Aborted / Completed; DispatcherOperation<T> adds Result.

Dispatcher.UIThread.Post is the default choice from background code. InvokeAsync/Invoke take the same priorities; Invoke on the UI thread itself runs inline.

DispatcherPriority (low to high)

SystemIdle (-5) < ApplicationIdle (-4) < ContextIdle (-3) < Background (-2) < Input (-1) < Default (0) < Loaded (1) < UiThreadRender (2) < Render (4) < BeforeRender (5) < AsyncRenderTargetResize (6) < Normal (8) < Send (9). Also public: Inactive, Invalid, MaxValue; DataBind is obsolete.

NeedPriority
Normal UI update from workerDefault (omit)
Bulk/low-importance updates that must not starve input/renderBackground
Run after layout/render settleLoaded or Background
Idle housekeepingApplicationIdle / SystemIdle

Posting thousands of Normal+ items starves input and rendering — batch, or use Background.

async/await in ViewModels

ViewModel commands start on the UI thread; the continuation after await resumes there too.

csharp
public partial class SearchViewModel : ObservableObject   // CommunityToolkit.Mvvm
{
    private CancellationTokenSource? _cts;

    [ObservableProperty] private bool _isBusy;
    [ObservableProperty] private string? _error;

    [RelayCommand]
    private async Task SearchAsync(string query)
    {
        _cts?.Cancel();
        _cts?.Dispose();
        _cts = new CancellationTokenSource();
        var ct = _cts.Token;

        IsBusy = true; Error = null;                  // UI thread
        try
        {
            var results = await Task.Run(() => _service.Search(query, ct), ct); // worker
            Results.Clear();                           // back on UI thread
            foreach (var r in results) Results.Add(r);
        }
        catch (OperationCanceledException) { /* superseded */ }
        catch (Exception ex) { Error = ex.Message; }
        finally { IsBusy = false; }
    }

    public ObservableCollection<ResultItem> Results { get; } = new();
}

Patterns:

  • Prefer truly async APIs (HttpClient, Stream.ReadAsync) over Task.Run wrappers.
  • Task.Run only for CPU-bound work. Inside the lambda do not touch controls or bound properties.
  • async void only for event handlers; wrap the body in try/catch — unhandled exceptions crash the app. Prefer async commands.
  • Fire-and-forget from sync code: _ = DoAsync(); with exceptions handled inside DoAsync.
  • Property changes raised on a background thread propagate to bindings on that thread and will throw — set bound properties only on the UI thread.

ConfigureAwait

WhereGuidance
ViewModel / View / code-behind touching UI stateDo not use ConfigureAwait(false) — you need the UI-thread continuation
Library / service / repository code with no UI accessConfigureAwait(false) on every await is fine and avoids UI-thread hops
After ConfigureAwait(false) in a mixed methodContinuation is on a pool thread — marshal with Dispatcher.UIThread.Post/InvokeAsync before UI work

Background Work + Progress

csharp
// Progress<T> captures SynchronizationContext.Current at construction.
// Construct it on the UI thread so Report() callbacks run on the UI thread.
var progress = new Progress<int>(p => ProgressValue = p);

await Task.Run(async () =>
{
    for (var i = 0; i <= 100; i++)
    {
        ct.ThrowIfCancellationRequested();
        await DoStepAsync(i, ct);
        ((IProgress<int>)progress).Report(i);
    }
}, ct);

Alternative without Progress<T>: Dispatcher.UIThread.Post(() => ProgressValue = p). Throttle high-frequency reporting (e.g. report every N ms or only when the value changes) — each report is one dispatcher item.

IProgress<T> is easy to test; Progress<T> created off the UI thread invokes callbacks on the thread pool.

ObservableCollection from Background Threads

ObservableCollection<T> is not thread-safe, and CollectionChanged is raised on the mutating thread; bindings then touch controls from that thread and throw.

csharp
// WRONG — worker mutates bound collection
await Task.Run(() => { foreach (var x in LoadItems()) Items.Add(x); });

// RIGHT — compute off-thread, mutate on UI thread
var loaded = await Task.Run(() => LoadItems().ToList());
foreach (var x in loaded) Items.Add(x);                       // after await = UI thread

// RIGHT — mutate from a worker via the dispatcher
Dispatcher.UIThread.Post(() => Items.Add(item));

// Large batches — chunk to keep UI responsive
foreach (var chunk in loaded.Chunk(200))
{
    foreach (var x in chunk) Items.Add(x);
    await Dispatcher.UIThread.InvokeAsync(() => { }, DispatcherPriority.Background); // let input/render run
}

For large replacements, prefer swapping the whole collection property (one change notification) over thousands of Add calls. DynamicData (SourceCache/SourceList + ObserveOn(...) to the UI thread) is a common alternative for streaming data; Rx ObserveOn needs a scheduler that targets the UI dispatcher; ReactiveUI.Avalonia configures RxApp.MainThreadScheduler for this (verify against the package version).

DispatcherTimer

csharp
private readonly DispatcherTimer _timer = new() { Interval = TimeSpan.FromSeconds(1) };

public Clock()
{
    _timer.Tick += (_, _) => Now = DateTime.Now;   // Tick runs on the timer's dispatcher
    _timer.Start();
}

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
    base.OnDetachedFromVisualTree(e);
    _timer.Stop();                                  // always stop — a running timer keeps handlers alive
}

// One-shot / loop helpers (return IDisposable; dispose to cancel)
DispatcherTimer.RunOnce(() => Hint.IsVisible = false, TimeSpan.FromSeconds(3));
using var loop = DispatcherTimer.Run(() => { Poll(); return true; }, TimeSpan.FromSeconds(5)); // return false to stop

Constructors: (), (DispatcherPriority), (DispatcherPriority, Dispatcher), (TimeSpan, DispatcherPriority, EventHandler), (TimeSpan, DispatcherPriority, Dispatcher), (TimeSpan, DispatcherPriority, Dispatcher, EventHandler). Members: Interval, IsEnabled, Tag, Dispatcher (get-only), Start(), Stop(), Tick.

  • Tick handlers run on the UI thread, so long work in Tick freezes the UI; for slow work start a Task and marshal results.
  • Tick is not reentrant-safe for async work: async void tick handlers can overlap if work exceeds Interval — guard with a flag.
  • For non-UI periodic work use PeriodicTimer or System.Threading.Timer and marshal results with Post.
  • (12.0+) DispatcherTimer() binds to Dispatcher.CurrentDispatcher — create it on the UI thread, or pass Dispatcher.UIThread explicitly. (11.x) always the UI thread.

Cancellation

  • One CancellationTokenSource per operation; cancel the previous one before starting a new (search-as-you-type).
  • Pass the token to the async API and Task.Run(..., ct); call ct.ThrowIfCancellationRequested() in loops.
  • Catch OperationCanceledException separately — it is not an error.
  • Dispose the CTS when replaced; cancel it on view/ViewModel teardown (Unloaded/DetachedFromVisualTree/IDisposable).
  • Dispatcher.InvokeAsync(Action, DispatcherPriority, CancellationToken) cancels a queued operation; it does not interrupt a running delegate. DispatcherOperation.Abort() likewise only works before it starts.

AvaloniaSynchronizationContext

Installed automatically on the UI thread (AvaloniaSynchronizationContext.AutoInstall is true by default; InstallIfNeeded() is called for you). Post queues to the dispatcher without blocking; Send runs synchronously (inline when already on the dispatcher thread). This is why await resumes on the UI thread and Progress<T> works.

  • Pool threads have no context, so await after Task.Run inner code resumes on the pool; only the outer await in a UI method resumes on the UI thread.
  • (12.0+) new AvaloniaSynchronizationContext() uses Dispatcher.CurrentDispatcher (the calling thread's dispatcher), not Dispatcher.UIThread. Overloads: (DispatcherPriority) and (Dispatcher, DispatcherPriority) are public. Do not construct one on a worker thread expecting UI-thread behavior.

Avalonia 12 Threading Changes

ChangeDetail
(12.0+) Multiple dispatchersOne Dispatcher per thread. Dispatcher.CurrentDispatcher (static, current thread), Dispatcher.FromThread(Thread) (returns Dispatcher?), AvaloniaObject.Dispatcher (get-only; the dispatcher of the thread that created the object)
Dispatcher.UIThreadStill valid and recommended for application code. Library/control authors should prefer AvaloniaObject.Dispatcher / Dispatcher.CurrentDispatcher
AvaloniaObject.CheckAccess() / VerifyAccess()Delegate to that object's Dispatcher — works on any AvaloniaObject
DispatcherTimer, AvaloniaSynchronizationContextDefault to the current dispatcher, not the UI thread. Create on the right thread or pass the dispatcher explicitly
InvokeAsync captures ExecutionContextAsyncLocal<T>, culture, impersonation flow from caller to the callback. Do not rely on callbacks seeing the UI thread's ambient context
Dispatcher.Yield() / Yield(priority) (static), Dispatcher.Resume() / Resume(priority)Awaitables returning to the dispatcher at a priority (default Background); Yield uses the current dispatcher
Android dispatcherImplemented over Android Looper/MessageQueue (rebuilt Android backend) — no code change needed
Managed dispatcherGained explicit background-processing support; internal, no app-level API change
csharp
// (12.0+) let input/render run in the middle of a long UI-thread loop
foreach (var item in items)
{
    Process(item);
    await Dispatcher.Yield();                       // continuation at Background priority
}

// (12.0+) library code: use the control's own dispatcher
void SafeUpdate(AvaloniaObject target, Action a)
{
    if (target.CheckAccess()) a();
    else target.Dispatcher.Post(a);
}

Dispatcher.Yield() is for UI-thread loops. Truly heavy work still belongs in Task.Run.

Testing

Headless tests run the Avalonia dispatcher on the test's UI thread; await dispatcher work rather than using Thread.Sleep. See the avalonia-testing skill for the headless test attributes, session setup and pumping queued jobs. Keep ViewModel async logic injectable (TimeProvider, services returning Task) so most tests need no dispatcher.

Common Mistakes

  • Updating a control/bound property from Task.Run or a System.Threading.Timer callback — throws "Call from invalid thread". Use Dispatcher.UIThread.Post(...).
  • .Result / .Wait() / GetAwaiter().GetResult() on the UI thread — deadlock when the awaited task needs the UI thread. Make the call chain async.
  • ConfigureAwait(false) in ViewModel code, then setting properties — continuation is on a pool thread. Remove it, or marshal explicitly.
  • Dispatcher.UIThread.Invoke from a worker while the UI thread waits on the worker — deadlock. Prefer Post/InvokeAsync.
  • Mutating ObservableCollection in Task.Run — see above; build a list off-thread, add on UI thread.
  • Posting per-item at high priority in a tight loop — starves input/render. Batch, or use DispatcherPriority.Background.
  • async void outside event handlers — exceptions crash the process and cannot be awaited/tested.
  • Not stopping DispatcherTimer on view teardown — leaks the view and keeps ticking.
  • Creating DispatcherTimer / AvaloniaSynchronizationContext on a worker thread in 12 — binds to that thread's dispatcher; ticks never fire on the UI. Create on the UI thread or pass Dispatcher.UIThread.
  • Assuming InvokeAsync callbacks see no ambient context in 12 — ExecutionContext now flows; AsyncLocal values from the caller are visible.
  • Progress<T> constructed on a worker thread — callbacks run on the pool. Construct on the UI thread.
  • Forgetting cancellation of superseded work — stale results overwrite newer ones; cancel the old CTS and ignore OperationCanceledException.
  • Expecting multiple UI threads to work in 12 — multiple dispatchers exist, multi-threaded UI is unsupported.

Sources

Veelgestelde vragen

Wat doet de AI-skill Avalonia Threading?

Use when code touches Avalonia threading — updating UI from background threads, Dispatcher.UIThread Post/InvokeAsync/Invoke, CheckAccess/VerifyAccess ("Call from invalid thread"), DispatcherPriority, async/await in ViewModels, ConfigureAwait, DispatcherTimer, progress reporting, cancellation, ObservableCollection updates off the UI thread, AvaloniaSynchronizationContext, or the Avalonia 12 multi-dispatcher changes (Dispatcher.CurrentDispatcher, Dispatcher.Yield).

Waarom Avalonia Threading gebruiken in TypingMind?

Omdat je deze skill één keer installeert en met elk model kunt gebruiken. Avalonia Threading bestaat uit gewone Markdown in plaats van aanbiederspecifieke code. Dezelfde skill werkt dus met GPT-5, Claude, Gemini, Grok of een lokaal model, en je kunt midden in een chat van model wisselen zonder dat de skill niet meer werkt. TypingMind werkt met je eigen API-sleutels, dus je betaalt aanbieders rechtstreeks in plaats van een abonnement per gebruikersplek. Je skills en chats blijven in je eigen opslag.

Hoe installeer ik Avalonia Threading in TypingMind?

Open Plug-ins → Skills → Installeren vanaf GitHub in TypingMind en plak https://github.com/linuxdevel/Avalonia-skills/tree/main/skills/avalonia/avalonia-threading. TypingMind leest het bestand SKILL.md en installeert het als een skill die je per chat kunt inschakelen.

Welke AI-modellen kunnen Avalonia Threading gebruiken?

Elk model dat je in TypingMind koppelt. AI-skills bestaan uit gewone Markdown-instructies in plaats van aanbiederspecifieke code. GPT, Claude, Gemini, Grok en lokale modellen kunnen deze skill dus allemaal laden wanneer een verzoek erbij past.

Hoeveel AI-modellen kan ik met Avalonia Threading gebruiken?

Zoveel als je wilt. Zolang een model skills ondersteunt, kun je Avalonia Threading ermee gebruiken: GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama en meer. Allemaal in TypingMind met je eigen API-sleutels.

Is de AI-skill Avalonia Threading gratis?

Ja. Deze skill is door linuxdevel op GitHub gepubliceerd onder de licentie MIT. Je betaalt alleen je eigen AI-aanbieder voor de tokens die je gebruikt.

Wat zijn AI-skills?

Een AI-skill is een herbruikbare bundel instructies die een AI-model leert hoe het één specifieke taak moet uitvoeren. Deze volgt het open Agent Skills-formaat: een bestand met de naam SKILL.md met een naam en beschrijving, plus eventuele scripts, sjablonen of referentiebestanden die het model nodig heeft. Het model leest de instructies alleen als je verzoek bij de skill past. Een geïnstalleerde skill kost dus niets zolang deze niet wordt gebruikt.

Waarin verschillen AI-skills van plug-ins of MCP-servers?

Een plug-in of MCP-server geeft een model nieuwe tools om aan te roepen: code die ergens wordt uitgevoerd en een resultaat teruggeeft. Een AI-skill geeft het model juist kennis en een werkwijze: hoe het een taak moet aanpakken, welke stappen het moet volgen en hoe goede uitvoer eruitziet. Skills bestaan uit gewone Markdown en hebben dus geen server, API-sleutel of runtime nodig. Ze werken met elk model.

Toon alles

Maak nu je eigen AI-werkruimte

Schrijf je in voor onze nieuwsbrief en blijf op de hoogte van nieuwe functies en toekomstige weggeefacties 👇