Asp. NET Tutorial: WaitHandle class

Source: Internet
Author: User
Tags array object net thread
When the program runs, a problem is encountered, if the WaitHandle array is more than 64 elements, the WaitHandle object's Watiall method strikes. Later, in order to run the program, I had to think of a stupid way: first create two WaitHandle objects in the array, and then use the loop two two to run the task.

I'm using it recently. NET Write program encountered a problem: There are n unrelated tasks to run in the thread pool, but there is a thread to wait for n tasks to complete before continuing. And this n is an unknown, it can be very large (hence the idea of using a thread pool instead of manually going to the new Therad). Turned over. NET class Library, a class called WaitHandle is found. The use of this class is interesting, you need to create a WaitHandle object for each thread and put them in an array, and then use the WaitAll method in the WaitHandle class to wait for these WaitHandle to be called set methods. (The code is not written, you can refer to the MSDN http://msdn.microsoft.com/zh-cn/library/system.threading.waithandle.aspx)

I thought it was a bit complicated, but I tried it. When the program runs, a problem is encountered, if the WaitHandle array is more than 64 elements, the WaitHandle object's Watiall method strikes. Later, in order to run the program, I had to think of a stupid way: first create two WaitHandle objects in the array, and then use the loop two two to run the task. The idea of the code is probably the following:

Waithandle[] handles = new Waithandle[]{new AutoResetEvent (false), new AutoResetEvent (false)};int times = (int) n/2;int i;   for (i = 0; I < times; i++) {ThreadPool.QueueUserWorkItem (new WaitCallback (tasks[i*2)), handles[0]); ThreadPool.QueueUserWorkItem (New WaitCallback (tasks[i*2+1]), handles[1]); WaitHandle.WaitAll (handles);} if (I*2 < N) {ThreadPool.QueueUserWorkItem (new WaitCallback (tasks[i*2 + 1]), handles[0]); WaitHandle.WaitAny (handles);}


While the code is more complex to write, it can at least guarantee that there is no problem at runtime. But it's obviously not kiss! to write code. So asked the master, said there is a RegisterWaitForSingleObject method, but a look at this method of the parameter list is enough to make people dizzy. A little nostalgic Java, remember that there is a Java Countdownlatch class, create a class when you assign an initial value x, and then call the main thread await, thread pool running threads call the countdown method. You can implement the main thread wait for the X-Countdown method call to continue. There is no limit to 64 WaitHandle, and there is no need to study that RegisterWaitForSingleObject method. But the problem is. NET does not have such a thing, can only do-it-yourself.

Class Countdownlatch {Private object lockobj;private int counts;
public Countdownlatch (int counts) {this.counts = counts;}
public void Await () {lock (lockobj) {while (Counts > 0) {monitor.wait (lockobj);}}}
public void Countdown () {lock (lockobj) {counts--; Monitor.pulseall (Lockobj);}}


With this thing, the code above can be changed a little bit.

Countdownlatch CDL = new Countdownlatch (n); for (int i = 0; i < N; i++) {ThreadPool.QueueUserWorkItem (Tas Ks[i]), CDL); Cdl. Awati ();


And for the Code of the task, at the end of the call to the WaitHandle set method to the countdown method of the Countdownlatch class.
Finally, I would like to say that there is no need to put the idea into a binding. NET or Java, mutual reference will make the idea more open.

However, there is a truth to say is that the Java class library in some areas do better than. Net.



Related Article

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.