When to Use Dispatcher and When to Unsubscribe Events?

水 知 230 Reputation points
2025-12-12T07:16:13.5+00:00

Hi community!

I'm a new developer and I have serveral questions about using the dispatcher and unsubscribing the events.

When should I use dispatcher? When I browsing the sample codes I never found them using dispatcher, but sometimes my app will crash if I don't use dispatcher in some places.

When should I unsubscribe the events? For a templated control, if it's a child control of other control, should I unsubscribe the events when the control is being disposed? If so, where should I dispose them? The finalizer or somewhere else? And how to unsubscribe the anonymous functions?

Thanks for help!

Windows development | WinUI
0 comments No comments
{count} votes

Answer accepted by question author
  1. Harry Vo (WICLOUD CORPORATION) 3,820 Reputation points Microsoft External Staff Moderator
    2025-12-12T08:21:45.45+00:00

    Hi @水 知 , thanks for reaching us on this platform!

    When should I use dispatcher?

    In WinUI 3, UI elements must only be accessed from the UI thread. If code runs on a background thread (Task.Run, async database calls...) and tries to touch the UI, you will get exceptions. This is when you use Dispatcher.

    • You update UI from code running on a background thread
    • Your async method resumes on a non-UI thread

    When I browsing the sample codes I never found them using dispatcher

    Most sample code simply runs on the UI thread or uses async/await that automatically returns to UI context.

    but sometimes my app will crash if I don't use dispatcher in some places.

    Your app crashes because after await, it sometimes runs on a background thread, and background thread are not allowed to touch the UI. So, the dispatcher is needed to move the code back to the UI thread before updating controls.


    When should I unsubscribe events?

    You should unsubscribe events whenever the subscriber (your control or page) may be garbage-collected but the publisher lives longer.

    • If the event source outlives the subscriber, unsubscribe them.

    Controls that are children of ListView/GridView or templates must unsubscribe, because virtualization reuses and removes controls.

    Where to unsubscribe:

    • Do NOT rely on finalizers (~ClassName) because WinUI controls often never run them reliably.
    • Do NOT rely on Dispose() as controls in WinUI do not implement Dispose.

    Do unsubscribe in Unloaded or OnNavigatedFrom, depending on the control type.

    Here is an example for templated child control generated by Copilot:

    public MyChildControl()
    {
        Loaded += OnLoaded;
        Unloaded += OnUnloaded;
    }
    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        SomeService.DataChanged += OnDataChanged;
    }
    private void OnUnloaded(object sender, RoutedEventArgs e)
    {
        SomeService.DataChanged -= OnDataChanged;
    }
    

    For a templated control, if it’s a child control of another control, should I unsubscribe the events when the control is being disposed?

    Yes, always unsubscribe.

    "Being disposed" in WinUI is actually when Unloaded fires or the parent control removes the child.

    Finalizers should never be used for event cleanup as they are too late and unreliable in WinUI.

    Where to unsubscribe (mentioned above)


    And how to unsubscribe the anonymous functions?

    You cannot unsubscribe directly, because there is no reference to it.

    The correct approach is storing the delegate in a variable and unsubscribe later.

    Here is an example generated by Copilot:

    private RoutedEventHandler _handler;
    public MyControl()
    {
        _handler = (s, e) => DoWork();
        Button.Click += _handler;
    }
    public void Cleanup()
    {
        Button.Click -= _handler;
    }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2025-12-12T07:16:23.13+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    When to use a dispatcher typically depends on the context of your application, especially in UI programming. A dispatcher is used to manage the execution of tasks on a specific thread, often the UI thread. If you need to update UI elements from a background thread, you should use the dispatcher to ensure that these updates are performed on the UI thread. This prevents crashes and threading issues, which is likely why your app crashes in certain situations when you don't use it.

    As for unsubscribing from events, it is essential to unsubscribe from events to prevent memory leaks. If your control is a child of another control, you should unsubscribe from its events when the control is being disposed. This is typically done in the Dispose method of the control, rather than in the finalizer. The finalizer is not the best place for this because it may not be called in a timely manner, and it can lead to resource management issues. Unsubscribing in the Dispose method ensures that the event handlers are removed while the control is still in a valid state.


    References:


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.