ManualResetEvent WaitHandle.WaitAll不起作用ASP.NET [英] ManualResetEvent WaitHandle.WaitAll not working ASP.NET

查看:93
本文介绍了ManualResetEvent WaitHandle.WaitAll不起作用ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查以下代码.当我在IIS上运行以下代码时,它对我有用.但是,当我尝试远程运行它时,WaitHandle.WaitAll(resetEvents)没有阻塞,并且没有等待ThreadB的所有实例完成.知道可能是什么原因造成的吗?

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

}

推荐答案

如果至少有一个事件等待句柄处于非信号状态,则对WaitHandle.WaitAll的调用始终会阻塞调用线程. br/>
您根本不会注意到它,或者永远不会达到阻塞状态,因为所有线程都非常快地完成了.您永远都不知道哪个线程首先到达特定的执行点,而依赖执行顺序的尝试称为竞赛条件(http://en.wikipedia.org/wiki/Race_condition [ ^ ]).

要进行检查,请在"resetEvents[index].Set();"行之前添加对System.Threading.Thread.Sleep的调用. —您会发现阻塞是睡眠时间足够长.

不说这样的对Sleep的调用应该在现实生活的编程中使用.恰恰相反:尝试延迟解决任何逻辑问题将是绝对不正确的.您可以出于实验目的进行操作.

由于您的所有代码似乎都没有任何实际意义,因此我假设您全部是出于实验目的而做的.好吧,练习事情是件好事.祝你好运.

—SA
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


这篇关于ManualResetEvent WaitHandle.WaitAll不起作用ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆