Problem in thread pool implementation(C#3.0)

Posted by Newbie on Stack Overflow See other posts from Stack Overflow or by Newbie
Published on 2010-06-09T05:25:01Z Indexed on 2010/06/09 6:22 UTC
Read the original article Hit count: 113

Filed under:

Hi Experts,

I have done the below thread pool program but the problem is that the WaitCallBackMethod(here ThreadPoolCallback) is getting called 2 times(which ideally should be called 1ce). what is the misktake I am making?

public class Calculation
    {
         #region Private variable declaration
           ManualResetEvent[] factorManualResetEvent = null;
         #endregion

        public void Compute()
        {                
                factorManualResetEvent = new ManualResetEvent[2];

                for (int i = 0; i < 2; i++){ 
                factorManualResetEvent[i] = new ManualResetEvent(false);
                ThreadPool.QueueUserWorkItem(ThreadPoolCallback, i);}


           //Wait for all the threads to complete
           WaitHandle.WaitAll(factorManualResetEvent);  

         //Proceed with the next task(s)  
        NEXT_TASK_TO_BE_EXECUTED();        
       }

       #region Private Methods

        // Wrapper method for use with thread pool.
        public void ThreadPoolCallback(Object threadContext)
        {
            int threadIndex = (int)threadContext;
            Method1();
            Method2();          
           factorManualResetEvent[threadIndex].Set();
        }

        private void Method1 ()
            { //Code of method 1}

        private void Method2 ()
            { //Code of method 2 }

        #endregion
}

I am using C#3.0 Thanks

© Stack Overflow or respective owner

Related posts about c#3.0