GuidesDispose on a UI thread
Dispose on a UI thread
Dispose a resource on the SynchronizationContext that owns it, waiting for it or not.
Some resources, such as a UI control, must be disposed on the thread that owns them. A context disposable wraps the resource with the SynchronizationContext of that thread and runs the disposal there, whichever thread calls Dispose. This page shows how to dispose and wait, how to dispose without waiting, how to dispose asynchronously, and what to watch for.
Dispose and wait
Section titled “Dispose and wait”Disposable.Context calls the context’s Send and returns once the resource is disposed. The resource is disposed once, however often Dispose is called.
var log = new List<string>();var control = Disposable.Create(() => log.Add("control disposed"));
// uiContext is the SynchronizationContext captured on the UI thread.var disposable = Disposable.Context(control, uiContext);disposable.Dispose(); // Send: returns once the UI thread has disposed the controlDispose without waiting
Section titled “Dispose without waiting”Disposable.PostingContext calls the context’s Post, between OperationStarted and OperationCompleted, so Dispose returns at once and the context disposes the resource later.
List<string> log = [];var control = Disposable.Create(() => log.Add("control disposed"));
// uiContext is the SynchronizationContext captured on the UI thread.var disposable = Disposable.PostingContext(control, uiContext);
// Post: returns at once; the UI thread disposes the control laterdisposable.Dispose();Dispose asynchronously
Section titled “Dispose asynchronously”AsyncDisposable.Context posts the disposal to the context, and DisposeAsync completes once it has run.
var log = new List<string>();var viewModel = AsyncDisposable.Create(() => log.Add("view model disposed"));
// uiContext is the SynchronizationContext captured on the UI thread.var disposable = AsyncDisposable.Context(viewModel, uiContext);await disposable.DisposeAsync(); // Post: completes once the UI thread has disposed itPitfalls
Section titled “Pitfalls”When the context’s Post throws, Dispose rethrows and the resource is not disposed:
List<string> log = [];var control = Disposable.Create(() => log.Add("control disposed"));
// uiContext's dispatcher has shut down: its Post throws.var disposable = Disposable.PostingContext(control, uiContext);
var failed = false;try{ disposable.Dispose();}catch (InvalidOperationException){ failed = true; // log is empty: the control is not disposed on this thread}- A context that refuses the work.
- You write
Disposable.PostingContext(control, uiContext)after the UI thread shut down. - What happens:
DisposecallsOperationCompletedand throws the context’s exception, and the control stays undisposed, because it must be disposed on its own thread. - Write instead: handle the exception where the UI shuts down.
- You write
- Sending to a UI thread that is waiting for you.
- You write
Disposable.Context(control, uiContext).Dispose()on a background thread the UI thread is waiting for. - What happens:
Sendwaits for the UI thread, which waits in turn, so neither moves. - Write instead:
Disposable.PostingContextorAsyncDisposable.Context.
- You write