"Writing high-quality Code C #" recommendation 72: Using semaphores in thread synchronization

Source: Internet
Author: User

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

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.