Back to list
christian289

threading-wpf-dispatcher

by christian289

ClaudeCode와 함께하는 .NET 개발 튜토리얼

1🍴 0📅 Jan 25, 2026

SKILL.md


name: threading-wpf-dispatcher description: Explains WPF Dispatcher priority system and threading patterns. Use when implementing background operations, maintaining UI responsiveness, or understanding task scheduling order.

WPF Dispatcher Threading

Priority Levels (High to Low)

PriorityValueUse Case
Send10Synchronous (avoid - deadlock risk)
Normal9Standard operations
DataBind8Binding updates
Render7Rendering operations
Loaded6Loaded event handlers
Input5User input processing
Background4Background tasks (UI stays responsive)
ContextIdle3After context operations
ApplicationIdle2App idle (cache cleanup)
SystemIdle1System idle
Inactive0Disabled

Common Patterns

Background Work (UI Responsive)

await Dispatcher.InvokeAsync(() =>
{
    // This runs between input processing
    ProcessNextChunk();
}, DispatcherPriority.Background);

After Render Complete

await Dispatcher.InvokeAsync(() =>
{
    // Runs after layout/render
    ScrollIntoView(lastItem);
}, DispatcherPriority.Loaded);

Idle Cleanup

Dispatcher.InvokeAsync(() =>
{
    // Low priority cleanup
    ClearUnusedCache();
}, DispatcherPriority.ApplicationIdle);

Yielding Pattern

Keep UI responsive during long operations:

public async Task ProcessLargeDataAsync(IList<Item> items)
{
    for (int i = 0; i < items.Count; i++)
    {
        Process(items[i]);

        // Yield every 100 items to process pending input
        if (i % 100 == 0)
        {
            await Dispatcher.Yield(DispatcherPriority.Background);
            UpdateProgress(i, items.Count);
        }
    }
}

Threading Rules

// Check if on UI thread
if (!Dispatcher.CheckAccess())
{
    Dispatcher.Invoke(() => UpdateUI());
    return;
}

// From background thread
await Task.Run(() =>
{
    var result = HeavyComputation();

    // Marshal back to UI
    Dispatcher.Invoke(() => DisplayResult(result));
});

Avoid

// ❌ Send priority - can cause deadlock
Dispatcher.Invoke(() => {}, DispatcherPriority.Send);

// ❌ Blocking UI thread
Thread.Sleep(1000);

// ✅ Use async/await instead
await Task.Delay(1000);

Score

Total Score

65/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

1ヶ月以内に更新

+10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

+5

Reviews

💬

Reviews coming soon