Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#

Solved: “The number of WaitHandles must be less than or equal to 64″

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
4 Jan 2011CPOL1 min read 44.9K   5   4
Solved: “The number of WaitHandles must be less than or equal to 64″

I came across this solution in the following blog post: http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-threadpool. The solution though was hidden pretty far down into the comments, so let me try here to bring you a bit more directly to the solution.

So your initial problem was, “The number of WaitHandles must be less than or equal to 64?. Where does it come from? Well, as the error messages says, you are limited to 64 WaitHandles. More specifically, you can’t create more than 64 ManualResetEvent objects. So in order to work our way around that problem, we have to stop relying on one ManualResetEvent object per thread to decide if all threads are completed or not.

The solution is to introduce a member variable that keep track of the number of threads yet to finish. When that variable reaches zero, we signal on one specific ManualResetEvent object that all threads are done. In order for this to work, you need to make the decrement of the pending threads to work as an atomic operation. The way to do so is by using Interlocked.Decrement, which ensures atomic decrement.

An example of how this pattern can be implemented:

C#
using System;
using System.Threading;

namespace ThreadPoolTest
{
    class Program
    {
        private static int _numerOfThreadsNotYetCompleted = 100;
        private static ManualResetEvent _doneEvent = new ManualResetEvent(false);

        private static void Main(string[] args)
        {
            for (int threadNumber = 0; threadNumber < 100; threadNumber++)
                ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), 
					(object)threadNumber);

            _doneEvent.WaitOne();

            Console.WriteLine("All done.");
        }

        private static void DoWork(object o)
        {
            try
            {
                Console.WriteLine("Thread number: {0}", (int)o);
            }
            finally
            {
                if (Interlocked.Decrement(ref _numerOfThreadsNotYetCompleted) == 0)
                    _doneEvent.Set();
            }
        }
    }
}

Using this, you can queue as many threads as you like, and still be able to wait for all of them to finish.


This article was originally posted at http://oddleifhalvorsen.com?p=161

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Software Innovation
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
theCPkid25-May-13 4:13
theCPkid25-May-13 4:13 
GeneralMy vote of 5 Pin
Reza Ahmadi3-Mar-12 20:12
Reza Ahmadi3-Mar-12 20:12 
GeneralMy vote of 5 Pin
Sayron6-Jan-12 6:10
Sayron6-Jan-12 6:10 
GeneralMy vote of 5 Pin
wuuhuu7-Jul-11 8:07
wuuhuu7-Jul-11 8:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.