Click here to Skip to main content
12,066,952 members (57,925 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

10K views
5 bookmarked
Posted

Working with ThreadPool

, 6 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A workaround for using more than 64 threads with ThreadPool and WaitHandle class.
Before going further, please check the MSDN link How to: Use a Thread Pool [^]. The above link will give you an idea on how to use Thread Pool.

Now in code given at the above link, in Main() method if you make a small change as given below:

const int FibonacciCalculations = 100; //Change its value to 100 from 10
The program will throw a NotSupportedException.

This is because a program can pass only 64 handles to WaitHandle class. So if you want to work with more than 64 threads simultaneously, this program won't work.

I worked out the above program to fulfill my requirement of creating more threads. I made few changes only in the Main() method.
Take a look:

static void Main()
        {
            const int FibonacciCalculations = 100;
 
            // One event is used for each Fibonacci object
            List<ManualResetEvent> doneEvents = null;
            Fibonacci[] fibArray = new Fibonacci[FibonacciCalculations];
            Random r = new Random();
            int totalCount = 0;
            // Configure and launch threads using ThreadPool:
            Console.WriteLine("launching {0} tasks...", FibonacciCalculations);
 
        ProcessThread://Label
            doneEvents = new List<ManualResetEvent>();
            for (int i = totalCount, j = 0; i < FibonacciCalculations && j < 64; i++, j++)
            {
                totalCount++;
 
                doneEvents.Add(new ManualResetEvent(false));
                Fibonacci f = new Fibonacci(r.Next(20, 40), doneEvents[j]);
                fibArray[i] = f;
                ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);
            }
 
            // Wait for all threads in pool to calculation...
            WaitHandle.WaitAll(doneEvents.ToArray());            
 
            if (totalCount < FibonacciCalculations)
                goto ProcessThread;
 
            Console.WriteLine("All calculations are complete.");
            // Display the results...
            for (int i = 0; i < FibonacciCalculations; i++)
            {
                Fibonacci f = fibArray[i];
                Console.WriteLine("Result {2} : Fibonacci({0}) = {1}", f.N, f.FibOfN, i);
            }
        }
Now run the program and it will work fine for 100 threads. In this manner, you can work with more than 64 threads.

I have used this method to achieve my goal, but if anyone else has a better way to achieve this, please share it with us.

Note: ManualResetEvent closing tag seen at the end of the code snippet is not part of the code snippet. I am not been able to remove those closing tags.

Thanks,
Nagendra.

License

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

Share

About the Author

nagendrathecoder
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
Generalah, its a limitation on the WaitHandle. Have a look at http:... Pin
Doc Lobster3-Nov-11 21:50
memberDoc Lobster3-Nov-11 21:50 
GeneralI think thats strange. There's no limit named by MSDN for qu... Pin
Doc Lobster3-Nov-11 7:57
memberDoc Lobster3-Nov-11 7:57 
GeneralRe: There is a limit of 64 handles given by MSDN. Please check R... Pin
nagendrathecoder3-Nov-11 20:12
membernagendrathecoder3-Nov-11 20:12 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.160208.1 | Last Updated 6 Nov 2011
Article Copyright 2011 by nagendrathecoder
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid