Click here to Skip to main content
13,005,798 members (77,580 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

27.6K views
5 bookmarked
Posted 4 Jan 2011

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

, 4 Jan 2011
Rate this:
Please Sign up or sign in to vote.
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:

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.


License

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

Share

About the Author

Oddleif Halvorsen
Technical Lead Software Innovation
Norway Norway
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
theCPkid25-May-13 4:13
membertheCPkid25-May-13 4:13 
GeneralMy vote of 5 Pin
Reza Ahmadi3-Mar-12 20:12
memberReza Ahmadi3-Mar-12 20:12 
GeneralMy vote of 5 Pin
Sayron6-Jan-12 6:10
memberSayron6-Jan-12 6:10 
GeneralMy vote of 5 Pin
wuuhuu7-Jul-11 8:07
memberwuuhuu7-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170627.1 | Last Updated 4 Jan 2011
Article Copyright 2011 by Oddleif Halvorsen
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid