Is the below thread pool implementation correct(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 5:32 UTC
Read the original article Hit count: 134

Filed under:

Hi Experts,

For the first time ever I have implemented thread pooling and I found it to be working.

But I am not very sure about the way I have done is the appropriate way it is supposed to be.

Would you people mind in spending some valuable time to check and let me know if my approach is correct or not?

If you people find that the approach is incorrect , could you please help me out in writing the correct version.

I have basicaly read How to use thread pool and based on what ever I have understood I have developed the below program as per my need

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
}

© Stack Overflow or respective owner

Related posts about c#3.0