Click here to Skip to main content
12,999,157 members (44,786 online)
Rate this:
Please Sign up or sign in to vote.
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?

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);              

    void ThreadB(object index_para)
        int index =(int)index_para;
        //Do tast here..functioncall();
        resetEvents[index] .Set();
Posted 13-Jun-11 13:48pm
Updated 13-Jun-11 15:54pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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 ([^]).

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.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 13 Jun 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100