This 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(ShowProgress, "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 the UpdateCallback
action to the SynchronizationContext
, the text box is updated on the same thread as the rest of the UI.
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.