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