Research on Lock and cross-thread UI operations in c,

Source: Internet
Author: User

Research on Lock and cross-thread UI operations in c,

This article only focuses on the lock used by multi-thread synchronization in C. To display the results more intuitively, you can write out the code for multi-threaded UI operations through the event during the demo. In fact, multi-thread synchronization has been used many times using the Synchronous lock method. I accidentally saw it in MSDN today. It is recommended to use: 1 private static readonly object locker1 = new object (); 2 private readonly object locker2 = new object (); note: the original text does not include readonly, Which I added later. I thought about their differences. Then I wrote a piece of code for testing. The test class code is as follows: copy the code /// <summary> /// parameters passed during cross-thread UI operations. In this article, to display messages, therefore, a simple encapsulation of // </summary> public class MyEventArgs: EventArgs {public readonly string Message = string. empty; public MyEventArgs (string msg) {this. message = msg ;}/// <summary> // test class, used to test the differences between the two locks // </summary> public class LockTest {// two locks private static readonly object Locker1 = new object (); private readonly object Loc Ker2 = new object (); // <summary> // delegate and event for cross-thread UI operations /// </summary> public delegate void MessageEventHandler (object sender, myEventArgs e); public event MessageEventHandler MessageEvent; public void OnMessage (MyEventArgs e) {if (this. messageEvent! = Null) MessageEvent (this, e);} // The variable to be locked. The effect of the two locks is private int num = 0 in different cases; // Instance Name private readonly string name; public LockTest (string Name) {name = Name;} // Method for executing the first lock public void AddNum1 () {lock (Locker1) {num = 0; ShowMessage () ;}// public void AddNum2 () {lock (Locker2) {num = 0; ShowMessage ();}} // lock some operations and display key messages to private void ShowMessage () {string msg in the UI of the main thread through events = ""; For (int I = 0; I <10; I ++) {num + = 1; msg = string. format ("Thread [{0}], the num value in instance [{1}] is [{2}]", Thread. currentThread. name, this. name, num); OnMessage (new MyEventArgs (msg); Thread. sleep (100);} msg = string. format ("====== Thread [{0}] execution completed ======", Thread. currentThread. name); OnMessage (new MyEventArgs (msg) ;}} the class used to copy the code test is finished. Start the test: first, test a single instance and multiple threads. The differences between the two locks are as follows: private void button#click (object sender, EventArgs E) {LockTest test = new LockTest ("LockTest 1"); test. messageEvent + = new LockTest. messageEventHandler (MessageCallBack); listBox1.Items. clear (); for (int I = 0; I <= 2; I ++) {Thread a = new Thread (new ThreadStart (test. addNum1);. name = I. toString ();. start () ;}} private void button2_Click (object sender, EventArgs e) {LockTest test = new LockTest ("LockTest 1"); test. messageEvent + = new LockTest. MessageEventHandler (MessageCallBack); listBox1.Items. clear (); for (int I = 0; I <= 2; I ++) {Thread a = new Thread (new ThreadStart (test. addNum2);. name = I. toString ();. start () ;}} outputs exactly the same results: it is concluded that there is no difference between the two locks when multiple threads access an instance. The following is a test of multiple instances (static lock): copy the code private void button3_Click (object sender, EventArgs e) {listBox1.Items. clear (); for (int I = 0; I <= 2; I ++) {LockTest test = new LockTest ("LockTest" + I. toString (); test. messageEvent + = new LockTest. messageEventHandler (MessageCallBack); Thread a = new Thread (new ThreadStart (test. addNum1);. name = I. toString ();. start () ;}} copied the code and got the result: it is concluded that in the face of a static lock, the thread is still waiting in line. Although it is not an instance, the lock is Unique, the thread only recognizes the lock, so the thread has no concurrency! Continue test (non-static lock): copy the code private void button4_Click (object sender, EventArgs e) {listBox1.Items. clear (); for (int I = 0; I <= 2; I ++) {LockTest test = new LockTest ("LockTest" + I. toString (); test. messageEvent + = new LockTest. messageEventHandler (MessageCallBack); Thread a = new Thread (new ThreadStart (test. addNum2);. name = I. toString ();. start () ;}}: the result of copying the code is as follows: when a non-static lock is used, multiple threads are concurrent and work together. In fact, the test results can be guessed before. However, if you don't test the results, you will always feel that the test is complete! Form, used for Event Callback, the code displayed in the UI is here: copy the code delegate void MessageHandler (string msg); public void MessageCallBack (object sender, MyEventArgs e) {MessageHandler handler = new MessageHandler (ShowMessage); this. invoke (handler, new object [] {e. message});} public void ShowMessage (string msg) {this. listBox1.Items. add (msg );}

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.