.NET Core.NET Framework Acronym Glossary.NET Framework ADO.NET.NET Framework CLR.NET Framework Code Contracts.NET Framework Collections.NET Framework Custom Types.NET Framework DateTime parsing.NET Framework Dependency Injection.NET Framework Dictionaries.NET Framework Encryption / Cryptography.NET Framework Exceptions.NET Framework Expression Trees.NET Framework File Input/Output.NET Framework ForEach.NET Framework Garbage Collection.NET Framework Globalization in ASP.NET MVC using Smart internationalization for ASP.NET.NET Framework HTTP clients.NET Framework HTTP servers.NET Framework Introduction.NET Framework JIT compiler.NET Framework JSON Serialization.NET Framework LINQ.NET Framework Managed Extensibility.NET Framework Memory management.NET Framework Networking.NET Framework NuGet packaging system.NET Framework Platform Invoke.NET Framework Process and Thread affinity setting.NET Framework Reading and writing Zip files.NET Framework ReadOnlyCollections.NET Framework Reflection.NET Framework Regular Expressions (System.Text.RegularExpressions).NET Framework Serial Ports.NET Framework Settings.NET Framework SpeechRecognitionEngine class to recognize speech.NET Framework Stack and Heap.NET Framework Strings.NET Framework Synchronization Contexts.NET Framework System.Diagnostics.NET Framework System.IO.NET Framework System.IO.File class.NET Framework System.Net.Mail.NET Framework System.Reflection.Emit namespace.NET Framework System.Runtime.Caching.MemoryCache (ObjectCache).NET Framework Task Parallel Library (TPL).NET Framework Task Parallel Library (TPL) API Overviews.NET Framework Threading.NET Framework TPL Dataflow.NET Framework Unit testing.NET Framework Upload file and POST data to webserver.NET Framework Using ProgressT and IProgressT.NET Framework VB Forms.NET Framework Work with SHA1 in C Sharp.NET Framework Write to and read from StdErr stream.NET Framework XmlSerializerJSON in .NET with Newtonsoft.JsonParallel processing using .NET framework

.NET Framework Synchronization Contexts

From WikiOD

Remarks[edit | edit source]

A Synchronization Context is an abstraction that allows consuming to code to pass units of work to a scheduler, without requiring awareness of how the work will be scheduled.

Synchronization contexts are traditionally used to ensure that code is run on a specific thread. In WPF and Winforms applications, a SynchronizationContext representing the UI thread is provided by the presentation framework. In this way SynchronizationContext can be thought of as a producer-consumer pattern for delegates. A worker thread will produce executable code (the delegate) and queue it or consumption by the UI message loop.

The Task Parallel Library provides features for automatically capturing and using synchronization contexts.

Execute code on the UI thread after performing background work[edit | edit source]

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.