Click here to Skip to main content
12,695,694 members (29,198 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

11.7K views
7 bookmarked
Posted

Using SynchronizationContext in Windows Forms

, 22 Mar 2013 MIT
Rate this:
Please Sign up or sign in to vote.
How to use SynchronizationContext in Windows Forms

Long back, I wrote a post about how to fix – System.InvalidOperationException – Cross-thread operation not valid exception, while we try to access user interface elements from a different thread, other than the thread (normally main thread), which created the user interface elements. .NET 2.0 brings us SynchoronizationContext which allows us to execute a section of code in the UI context (the thread that created the UI). Also, it allows us to specify if the background thread will block waiting for the UI thread (using Send) or will not block (using Post). SynchoronizationContext class can be used in Windows Forms, WPF, and ASP.NET, etc. For Windows Forms, you can get the UI context from the WindowsFormsSynchronizationContext.Current property. For WPF, the implementation is DispatcherSynchronizationContext class.

In the following example, I am updating the UI from a separate thread, without the _synchronizationContext.Post() method, it will throw an InvalidOperationException. Here is the implementation.

private ThreadStart _threadStart;
private Thread _thread;
private SynchronizationContext _synchronizationContext;

private void Form1_Load(object sender, EventArgs e)
{
    _synchronizationContext = 
        WindowsFormsSynchronizationContext.Current;
    _threadStart = new ThreadStart(LongProcess);
    _thread = new Thread(_threadStart);
    _thread.Start();
}

private void LongProcess()
{
    for (int i = 0; i < 100; i++)
    {
        Thread.Sleep(100);
        _synchronizationContext.Post(
            (o) => textBox1.Text = i.ToString(), null);
    }
}

When the value is assigned to textbox, if you look the threads window, you can find, it is running under main thread.

Threads window - UI thread

You can find more details about WindowsFormsSynchronizationContext class on MSDN.

Related Content

  1. System.InvalidOperationException – Cross-thread operation not valid
  2. Using background worker in C#
  3. Using Microsoft Ink Picture control
  4. How to use Anonymous methods for Control.Invoke
  5. How to get the current battery level in C#

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Anuraj Parameswaran
Technical Lead
India India
Working as Tech. Lead

My blog : dotnetthoughts.net.
You can follow me in twitter : @anuraj

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionThe added value? Pin
stadionwurst27-Mar-13 3:35
memberstadionwurst27-Mar-13 3:35 
AnswerRe: The added value? Pin
Anuraj Parameswaran24-Apr-13 19:35
memberAnuraj Parameswaran24-Apr-13 19:35 
AnswerRe: The added value? Pin
svasudevan3-Oct-15 17:12
membersvasudevan3-Oct-15 17:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 22 Mar 2013
Article Copyright 2013 by Anuraj Parameswaran
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid