Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
DotNETFrameworkNotesForProfessionals.pdf
Скачиваний:
32
Добавлен:
20.05.2023
Размер:
1.82 Mб
Скачать

Chapter 44: Synchronization Contexts

Section 44.1: Execute code on the UI thread after performing background work

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.

GoalKicker.com – .NET Framework Notes for Professionals

138