Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please examine the below Code. When I run the below code on IIS, its working for me. But when i try to run it remotely, the WaitHandle.WaitAll(resetEvents) is not blocking and is not waiting for all instances of ThreadB to finish. Any idea on what could be causing it?

C#
class ABC
{
    ManualResetEvent[] resetEvents;


    function StartThreadPool()
    {

        resetEvents =new ManualResetEvent[20];
        for(int i=0; i<20; i++)
        {
            resetEvents[i]=new ManualResetEvent(false);
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadB), (object) i);              
        }
        WaitHandle.WaitAll(resetEvents);

    }


    void ThreadB(object index_para)
    {
        int index =(int)index_para;
        //Do tast here..functioncall();
        resetEvents[index] .Set();
    }

}
Posted
Updated 13-Jun-11 15:54pm
v3

1 solution

The call to WaitHandle.WaitAll always block the calling thread if at least one the the event wait handles is in non-signaled state.

You simply don't notice it or never reach blocking state because all threads complete very quickly. You never know which thread reach certain point of execution first, and an attempt to rely on the order of execution is called race condition (http://en.wikipedia.org/wiki/Race_condition[^]).

To check this up, add a call to System.Threading.Thread.Sleep before the line "resetEvents[index].Set();" — you will observe the blocking is sleep time is long enough.

I don't say such call to Sleep should be use in real-life programming. Just the opposite: trying to resolve any logical problem with delay would be absolutely incorrect. You can do it just for experimental purpose.

As all you code does not seem to have any practical sense, I assume you do it all for experimental purposes yourself. Well, it's good to practice things; good luck.

—SA
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900