SynchronizationContext is the role of the transmitter in communication, which is the communication between one thread and another thread.
It is important to note that not every thread attaches synchronizationcontext this object, only the UI thread is always owned. so getting SynchronizationContext can only be done on the UI thread SynchronizationContext context = synchronizationcontext.current;
When will it be used?
In multi-threaded operation often need to cut back to a thread to work, and so on, and then cut back.
A child thread A is created in the main UI thread. A delegate event was added to the. The UI thread registers an event with the Class A thread, and modifies the properties on the UI, such as text, when the A thread triggers the event.
At this time, it is often necessary to use the control's Invoke method in the UI line Cheng thread-registered event method to access controls in the UI thread, because these registered event (delegate) method codes appear to be written in the UI thread's form class, but are actually registered in the event of child thread A. They are performed inside a child thread when a thread a triggers an event. In this way, we have to use the control's Invoke method in the registration event method of the main UI thread's class to access the control, which is cumbersome. We want to access the control directly in the registered event method, just like the control events of the system. Then you can use SynchronizationContext at this time.
Synchronizationcontext.send (SendOrPostCallback d,object state);
Synchronizationcontext.post (SendOrPostCallback d,object state);
D is a delegate (SendOrPostCallback) that has no return value and has an incoming parameter of type object;
The state is the argument (object) when executing the delegate;
Attention:
SynchronizationContext objects are not all attached, only the main thread of the UI is attached.
For the UI thread, how do you attach the SynchronizationContext object to the thread?
Before Form1 form = new Form1 (), the SynchronizationContext object is empty, and when the Form1 form is instantiated, the SynchronizationContext object is appended to the thread.
So you can get the answer: When the control object is created, the SynchronizationContext object is created and attached to the thread. All in use, be sure to wait for form InitializeComponent (); This is done before it can get a non-null object.
So what's the difference between SynchronizationContext's Sendt () and post () two methods?
Send () is a simple implementation (synchronous invocation) that invokes a delegate on the current thread. That is, the UI thread executes directly on the child thread, and the friend continues to execute after the UI thread finishes executing.
Post () is an online pool that invokes delegates for implementation (asynchronous invocation). This is a child thread that is looking for a thread from the thread pool to tune the UI thread, and the child thread does not wait for the UI thread to complete without directly executing its own code.
Example:
/// <summary> ///this needs to be defined in the main thread,///and get context = Synchronizationcontext.current in the main thread/// </summary> PrivateSynchronizationContext context; /// <summary> ///Form Loading/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidForm1_Load (Objectsender, EventArgs e) { //here is the premise of getting SynchronizationContext on the main threadContext =synchronizationcontext.current; //then we can make a thread.Thread thread =NewThread (NewThreadStart (Start)); Thread. IsBackground=true; Thread. Start (); } /// <summary> ///Threading Operations/// </summary> Private voidStart () { for(intI=0;i< -;++i) {//this way, the control of the main interface can be called normally.Context. Send (operation, i);//correct//apply directly as originally, because using the control will result in an errorOperation (i);//ErrorThread.Sleep ( -); } } /// <summary> ///Threading Operations/// </summary> /// <param name= "obj" ></param> Private voidOperation (Objectobj) {Textbox1.appendtext (obj. ToString ()+"\ r \ n"); }
Reference article: http://blog.csdn.net/iloli/article/details/16859605
"C #" SynchronizationContext Inter-thread synchronization