This article is a mirror article of machine translation, please click here to jump to the original article.

View: 22231|Reply: 0

[Communication] .net/c# SynchronizationContext for details

[Copy link]
Posted on 3/15/2018 11:51:07 AM | | | |
I have read many introductory articles on how to update the main interface in the background thread, and most of them use Control.Invoke and Control.BeginInvoke. These are good solutions, but there are two problems:

1. You must reference System.Windows.Forms, and then use System.Windows.Forms

2. The code structure is relatively messy. (Actually, this is also caused by #1)

Microsoft offers another more elegant solution, which is System.Threading. SynchronizationContext。 As you can see, it's not in namesapce System.Windows.Forms, so we can rightfully use it in BusinessLaryer, Controler, and even modules.

And it is very convenient to use, you only need to focus on the following two methods:

1. Send: Sends an interface update request to the main thread, blocking the current thread until it returns.

2. Post: Sends an interface update request to the main thread without blocking the current thread.

In fact, they are all the same method, except that send is synchronous and post is asynchronous


Before Form1 form = new Form1(), the SynchronizationContext object was empty, and when the Form1 form was instantiated, the SynchronizationContext object was appended to this thread. So the answer is that when the Control object is created, the SynchronizationContext object is also created and attached to the thread. All when using the form InitializeComponent(); Once this is done, it can get an object that is not NULL


Finally, the difference between the Sendt() and Post() methods of SynchronizationContext:

Send() is implemented by simply calling the delegate on the current thread (synchronous call). That is, the UI thread is directly called on the subthread to execute, and the subthread will continue to execute after the UI thread execution is completed.

Post() is implemented by calling a delegate on the thread pool (asynchronous call). This is because the subthread will find a thread from the thread pool to tune the UI thread, and the subthread will directly execute its own code without waiting for the UI thread to complete.
Test code:

Outcome:

UI main thread: 1
Thread: 5
SynchContext:1


Summary:The SynchronizationContext object on the UI thread, whether called on the main thread or in the thread, will be executed on the main thread, so when there is a lot of time-consuming code, it will cause the UI interface to freeze or fake death

In factThe UI thread uses not the SynchronizationContext class, but WindowsFormsSynchronizationContextThis Dongdong.





System.Threading.SynchronizationContext source code:


WindowsFormsSynchronizationContext source code:







Previous:Forty-seven ways to optimize a C# program
Next:.net/c# Shanghai Pai Shanghai Brand Assistant [with source code]
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com