1. Use the signaling mechanism to provide a simple example of thread synchronization
AutoResetEvent AutoResetEvent = new AutoResetEvent (false); private void Button1_Click (object sender, EventArgs e) { control.checkforillegalcrossthreadcalls = false; Thread twork = new Thread (() = { Label1. Text = "Thread start ..." + Environment.NewLine; Label1. Text + = "start to deal with some actual work" + Environment.NewLine; Omit work code Label1. Text + = "I began to wait for another thread to give me a signal before I was willing to go on" + Environment.NewLine; Autoresetevent.waitone (); Label1. Text + = "I'm going to do some work, and then it's over!" "; Omit work Code }); Twork.isbackground = true; Twork.start (); } private void Button2_Click (object sender, EventArgs e) { //Set event status to signaled state, allowing one or more waiting threads to continue AutoResetEvent.Set (); }
In the above example, click on button 1 to execute to Autoresetevent.waitone () no longer down, click on the button 2 will continue to execute down.
The difference between 2.AutoResetEvent and ManualResetEvent:
Using Autoresetevent,autoresetevent.set () allows only one thread to continue, while Manualresetevent,manualresetevent.set () allows multiple threads to continue executing.
Examples are as follows:
private void Button2_Click (object sender, EventArgs e) {//Set event status to signaled state, allowing one or more waiting threads to continue/ /autoresetevent.set (); Manualresetevent.set (); private void Button3_Click (object sender, EventArgs e) {Control.checkforillegalcrossthreadcall s = false; StartThread1 (); StartThread2 (); } private void StartThread1 () {Thread tWork1 = new Thread (() = {L Abel1. Text = "Thread 1 start ..." + Environment.NewLine; Label1. Text + = "start to deal with some actual work" + Environment.NewLine; Omit work code Label1. Text + = "I began to wait for another thread to give me a signal before I was willing to go on" + Environment.NewLine; Autoresetevent.waitone (); Manualresetevent.waitone (); Label1. Text + = "I'm going to do some work, and then it's over!" "; Omit work code}); Twork1.isbackground = true; Twork1.start (); } private void StartThread2() {Thread tWork2 = new Thread (() = {Label2. Text = "Thread 2 start ..." + Environment.NewLine; Label2. Text + = "start to deal with some actual work" + Environment.NewLine; Omit work code LABEL2. Text + = "I began to wait for another thread to give me a signal before I was willing to go on" + Environment.NewLine; Autoresetevent.waitone (); Manualresetevent.waitone (); Label2. Text + = "I'm going to do some work, and then it's over!" "; Omit work code}); Twork2.isbackground = true; Twork2.start (); }
3. Analog network communication
private void Button4_Click (object sender, EventArgs e) { control.checkforillegalcrossthreadcalls = false; Thread t = new Thread (() = {while (true) {
Principle: No 3 seconds to detect the reception, if not received a. Set (), the offline bool Re=a.waitone (+); if (re) { label3. Text = string. Format ("Time: {0},{1}", DateTime.Now.ToString (), "online"); } else { label3. Text = string. Format ("Time: {0},{1}", DateTime.Now.ToString (), "Offline");}} ); T.isbackground = true; T.start (); } private void Button5_click (object sender, EventArgs e) { a.set (); }
"Writing high-quality Code C #" recommendation 72: Using semaphores in thread synchronization