"C # Multithreaded Programming combat" 2.6 ManualResetEventSlim

Source: Internet
Author: User

This is better understood.

As the book says, as the house that has been opening the door, who will go in, who will close their doors, when out of the open.

There are three common methods:

Set ()//sets to have a signal, that is, to let the waiting thread do not continue to wait, wake up the waiting thread.

Reset ()//set to no signal means that no waiting thread becomes a waiting state, and the wait method needs to be matched.

Wait ()//waits for the thread to be blocked unless the signal "set method" is accepted to release

So the example in the book:

 classProgram {Static voidTest (stringThreadName,intseconds) {Console.WriteLine ($"{ThreadName} falls to sleep");            Thread.Sleep (timespan.fromseconds (seconds)); Console.WriteLine ($"{threadname} waits for the gates to open");                       _mainevent.wait (); Console.WriteLine ($"{ThreadName} enters the GATS"); }        StaticManualResetEventSlim _mainevent =NewManualResetEventSlim (false); Static voidMain (string[] args) {            varT1 =NewThread (() = Test ("Thread 1",5)); vart2 =NewThread (() = Test ("Thread 2",6)); varT3 =NewThread (() = Test ("Thread 3", A)); T1.            Start (); T2.            Start (); T3.            Start (); Thread.Sleep (Timespan.fromseconds (6)); Console.WriteLine ("The gates is now open");            _mainevent.set (); Thread.Sleep (Timespan.fromseconds (2));                        _mainevent.reset (); Console.WriteLine ("The Gates has been closed!"); Thread.Sleep (Timespan.fromseconds (Ten)); Console.WriteLine ("The fatres is now Ioen for the second time!");            _mainevent.set (); Thread.Sleep (Timespan.fromseconds (2)); Console.WriteLine ("The Gates has been closed");            _mainevent.reset ();        Console.readkey (); }    }

Run, and then match the interpretation on the book.

It's a good idea to think about it a little bit.

is to do something, it will be more trouble, but every step of the thread will be fully mastered.

When initialized to False

The general sequence of operations is

Reset ()//settings need to wait

Wait ()//actual waiting

Set ()//release thread

In general, initialization of ManualResetEventSlim is false.

If true, reset must be set manually once.

"C # Multithreaded Programming combat" 2.6 ManualResetEventSlim

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.