Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET IIS
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);              
        }
        WaitHandle.WaitAll(resetEvents);
 
    }
 

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

1 solution

Rate this: bad
good
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 (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
  Permalink  
v2

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

  Print Answers RSS
0 CHill60 320
1 Sarvesh Kumar Gupta 238
2 Sergey Alexandrovich Kryukov 173
3 OriginalGriff 163
4 DamithSL 153


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