threading-wpf-dispatcher
WPF Dispatcher Threading
Priority Levels (High to Low)
| Priority | Value | Use Case |
|---|---|---|
| Send | 10 | Synchronous (avoid - deadlock risk) |
| Normal | 9 | Standard operations |
| DataBind | 8 | Binding updates |
| Render | 7 | Rendering operations |
| Loaded | 6 | Loaded event handlers |
| Input | 5 | User input processing |
| Background | 4 | Background tasks (UI stays responsive) |
| ContextIdle | 3 | After context operations |
| ApplicationIdle | 2 | App idle (cache cleanup) |
| SystemIdle | 1 | System idle |
| Inactive | 0 | Disabled |
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);
More from christian289/dotnet-with-claudecode
converting-html-css-to-wpf-xaml
Converts HTML/CSS to WPF CustomControl XAML with correct patterns and common pitfall solutions. Use when transforming web designs to WPF, converting CSS animations to Storyboards, implementing CSS border-radius clipping, CSS pseudo-elements (::before/::after), or CSS transforms in XAML.
56publishing-wpf-apps
Guides WPF application publishing and installer options. Use when user mentions publish, deploy, release, packaging, or installer to help choose deployment method and installer technology.
14using-avalonia-collectionview
Provides CollectionView alternatives for AvaloniaUI using DataGridCollectionView and ReactiveUI. Use when filtering, sorting, or grouping collections in AvaloniaUI applications.
9designing-avalonia-customcontrol-architecture
Defines the basic solution structure for AvaloniaUI Desktop Applications using CustomControl. Use when creating new AvaloniaUI projects or designing stand-alone control styles with ControlTheme.
9using-xaml-property-element-syntax
Converts long inline XAML bindings to Property Element Syntax for better readability. Use when XAML binding expressions become too long or complex.
8managing-styles-resourcedictionary
Manages WPF Style definitions and ResourceDictionary patterns including BasedOn inheritance and resource merging. Use when building theme systems, organizing resources, or choosing between StaticResource and DynamicResource.
8