Communication Between Threads---SynchronizationContext

Source: Internet
Author: User

The time before the new year is really comfortable, without the usual project issued a version of the tension, the rest is only wandering on the Internet, haha!

Wandering this morning, in CodeProject found a good article, English good direct to http://www.codeproject.com/KB/threads/ Synchronizationcontext.aspx See, bad, will be on the look of my bar, hehe! (no direct translation, but probably the same idea)

Understanding SynchronizationContext

The SynchronizationContext class is a base class that provides free thread contexts with no synchronization. The purpose of the synchronization model implemented by this class is to enable asynchronous/synchronous operations within the common language runtime to take the correct behavior for different asynchronous models. This model also simplifies some of the requirements that managed applications must follow to work properly in different synchronization environments. The provider of the synchronization model can extend this class and provide its own implementation for these methods. (from MSDN)

The simple thing is to allow one thread to communicate with another, SynchronizationContext the role of the transmitter in the communication. In addition, there is a place to be clear, not every thread attaches synchronizationcontext this object, only the UI thread is always owned.

Here you may have a question: how do you attach a SynchronizationContext object to a thread for UI threads?! OK, let's start with the following code,

[STAThread]
static void Main ()
{
    application.enablevisualstyles ();
    Application.setcompatibletextrenderingdefault (false);
    Let's check the context where
    var context = synchronizationcontext.current;
    if (context = = null)
        MessageBox.Show ("No context to this thread");
    else
        MessageBox.Show ("We got a Context");
    Create a form
    Form1 form = new Form1 ();
    Let's check it again after the creating a form context
    = synchronizationcontext.current;
    if (context = = null)
        MessageBox.Show ("No context to this thread");
    else
        MessageBox.Show ("We got a Context");
    if (context = = null)
        MessageBox.Show ("No context to this thread");
    Application.Run (New Form1 ());
}

Run Result:

1, No context for this thread

2, We got a context

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.