Execute code on the UI thread after performing background work
suggest changeThis example shows how to update a UI component from a background thread by using a SynchronizationContext
void Button_Click(object sender, EventArgs args)
{
SynchronizationContext context = SynchronizationContext.Current;
Task.Run(() =>
{
for(int i = 0; i < 10; i++)
{
Thread.Sleep(500); //simulate work being done
context.Post(UpdateCallback, "Work complete on item " + i);
}
});
}
void UpdateCallback(object state)
{
// UI can be safely updated as this method is only called from the UI thread
this.MyTextBox.Text = state as string;
}
In this example, if you tried to directly update MyTextBox.Text inside the for loop, you would get a threading error. By posting UpdateCallback to the SynchronizationContext, the text box is updated on the same thread as the rest of the UI. SynchronizationContext.Current is non-null here only because the click handler runs on a UI thread that installed a context.
In practice, progress updates should be performed using an instance of System.IProgress<T>. The default implementation System.Progress<T> automatically captures the synchronisation context it is created on.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents