AutoResetEvent signal Lock WaitOne Set perform a thread exit pretty uncomfortable place.

Source: Internet
Author: User

There's a loop calling the thread to write the odd even number of programs

   Class Theadtest {//define a Stream object to receive open files private FileStream st;              Construction method Public Theadtest () {} public void Start () {///define two signal locks            AutoResetEvent Atea = new AutoResetEvent (false);            AutoResetEvent ateb = new AutoResetEvent (false);            Add the signal lock to the list list<autoresetevent> lst = new list<autoresetevent> (); Lst.            ADD (Atea); Lst.            ADD (ATEB);            Define output even threads with parameters thread Threven = new Thread (new Parameterizedthreadstart (Outputeven));            Threven.name = "even thread";            Two signal locks are passed into thread threven.start (LST);            Defines the output with parameters for odd threads thread throdd = new Thread (new Parameterizedthreadstart (outputodd));            Throdd.name = "Pole count thread";        Throdd.start (LST); } private void Outputeven (object lst) {AutoResetEvent Atea = (lst as list<autoresetevent>)            [0]; AuToresetevent ateb = (lst as list<autoresetevent>) [1];                for (int i = 0; I < + + + + 2) {//Set this thread to be no signal atea.waitone ();                Output Console.WriteLine (i);      st = File.Open (@ "C:/file.txt", FileMode.Open); The//st used to test simultaneous access to the file system.                  Close ();  Ateb.set ();              Set output odd thread has signal//thread.sleep (500); }} private void Outputodd (object lst) {AutoResetEvent Atea = (lst as List<autoresetev            ent>) [0];            AutoResetEvent ateb = (lst as list<autoresetevent>) [1];            Set even-numbered threads to output atea.set () first;                for (int i = 1; i < + + + + 2) {//Set this thread to be no signal ateb.waitone ();                Console.WriteLine (10000);     st = File.Open (@ "C:/file.txt", FileMode.Open); The//st used to test simultaneous access to the file system.                 Close (); AtEa.set ();             Set output even thread has signal//thread.sleep (500); }        }    }

  

At this point two thread execution methods are in the loop is not a problem, the program is interactive, Atea first execute, and then WaitOne, waiting for Ateb to send the set signal after execution.

To remove one of the loops,

for (int i = 1; i <; i + = 2)         //   {                //Set this thread to be                  ateb.waitone ();                Console.WriteLine (10000);                st = File.Open (@ "C:/file.txt", FileMode.Open);     The//st used to test simultaneous access to the file system                  . Close ();                 Atea.set (); Set output even thread has signal                  //thread.sleep (+);          //   }
Operation Result:

Ran three lines down, and it was
The Atea ran two times,
Ateb was run once.  no loops are run only once, and the thread ends.
Atea the loop even if sent
Ateb.set () signal also useless, the thread has been closed, run once closed, here are some very uncomfortable, also do not know is not even ignorant.
From an understanding point of view, the thread should always be there to start, give the set signal, the thread executes the program to start running, wait for the next set signal after execution, and should not exit.
So to use this knowledge point in the program, but also need to improve a lot. This problem last deal with the host computer encountered, was already aware of this problem, today because of the above notation, parameters and added a loop,
Causes oneself to appear the illusion, thought this kind of writing can give the signal execution, no signal does not execute, after executes the thread to hang instead of exits the function.

AutoResetEvent signal Lock WaitOne Set perform a thread exit pretty uncomfortable place.

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.