Wednesday, January 21, 2009

ThreadPool Wait For All Thread To Complete

I already discuss issue related to WaitHandle.WaitAll that it does not support wait for more 64 waithandle. In that article discuss about 64 chunks of thread to be executed parallel.

When you use ThreadPool and you combine it with 64 chunks of thread concept than you can not take advantage of ThreadPool. In ThreadPool all task wait for pool become empty.( Not fully). As ThreadPool have available thread then new task automatically executed.

So wait for more than 64 waithandle is needed, i found solution by following way.

C# Code.

using System;
using System.Collections.Generic;
using System.Threading;
namespace ThreadPoolWaitForAllThread
{
    class Program
    {
        static void Main(string[] args)
        {
            List<ManualResetEvent> events = new  List<ManualResetEvent>();
            for (int i = 0; i < 100; i++)
            {
                ThreadPoolObj obj = new ThreadPoolObj();
                obj.ObjectID = i;
                obj.signal =  new ManualResetEvent(false);
                events.Add(obj.signal);
                WaitCallback callback = new WaitCallback(ThreadFunction);
                ThreadPool.QueueUserWorkItem(callback,obj);
            }          
           WaitForAll(events.ToArray());
           Console.WriteLine("Compelted");
           Console.ReadLine();
        }
        static bool WaitForAll(ManualResetEvent[] events)
        {
            bool result = false;
            try
            {
                if (events != null)
                {
                    for (int i = 0; i < events.Length; i++)
                    {
                        events[i].WaitOne();
                    }
                    result = true;
                }
            }
            catch
            {
                result = false;
            }
            return result;
        }
        static void ThreadFunction(object threadobj)
        {
            ThreadPoolObj obj = threadobj as ThreadPoolObj;
            if (obj != null)
            {
                Console.WriteLine(obj.ObjectID.ToString());
                Thread.Sleep(2000); // Just Wait To Show Syncronization
                obj.signal.Set();
            }
        }
    }
    class ThreadPoolObj
    {
        public int ObjectID;
        public ManualResetEvent signal;
    }
}
By this way you can wait for more than 64 Waithandle.

Same function you can use anywhere even in case of article (WaitHandle.WaitAll Limitation). After use of this function you can avoid logic of 64 chunks, but if your thread consume more resource than it is better to run as chunks of thread instead of starting all thread at once. ThreadPool maintain this thing automatically.

Please give your comment on this.

3 comments:

Ronny said...

IS bad. :)
Your Thread run one after another.

MultiThread is better with 64 WaitHandles

Ronny

dotnetstep said...

This is not really bad. I know that WaitHandles has limitation of 64. This solution is for who must need to waitfor more then 64 threads.

Actually thread is not run one after another all thread started working as i used threadpool that depend on threadpool size.

WaitForAll function WaitFor single thread to complete the task , it does not mean that other thread not working. If they working and they completed their task then when in loop it comes to check for signal and it moves to next thread as all ready signal set.

If any body have other solution please suggest me.

For Limitation of 64 wait handle and how to solve it using 64 chucks i explained at following location

http://dotnetstep.blogspot.com/2009/01/waithandlewaitall-limitation.html

Unknown said...

I am not able to follow your code. I am not an expert at .NET but am familiar with general terms. Can you explain what your code does?