有没有更好的方式来等待排队的主题? [英] Is there a better way to wait for queued threads?

查看:155
本文介绍了有没有更好的方式来等待排队的主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的方式来等待排队线程之前执行另一个进程



目前我在做:

  this.workerLocker =新的对象(); //全局变量
this.RunningWorkers = arrayStrings.Length; //全局变量

//初始化过程

的foreach(在arrayStrings字符串someString)
{
ThreadPool.QueueUserWorkItem(this.DoSomething,someString) ;
Thread.sleep代码(100);
}

//等待执行的所有排队的线程
锁(this.workerLocker)//全局变量(对象)
{
,同时(这.RunningWorkers大于0)
{
Monitor.Wait(this.workerLocker);
}
}

//做别的
Console.WriteLine(END);




  //方法DoSomething的()定义
公共无效DoSomething的(对象数据)
{
//做一个缓慢的过程...




锁(this.workerLocker)
{
this.RunningWorkers--;
Monitor.Pulse(this.workerLocker);
}
}


解决方案

您可能想看看的AutoResetEvent和ManualResetEvent的。



这些都是为了正是这种情况(如等待一个线程池线程完成,做东西之前)。



您会做这样的事情:

 静态无效的主要(字串[] args)
{
名单,LT; ManualResetEvent的> resetEvents =新的List<&ManualResetEvent的GT;();
的foreach(在Enumerable.Range VAR×(1,WORKER_COUNT))
{
ManualResetEvent的resetEvent =新的ManualResetEvent();
ThreadPool.QueueUserWorkItem(DoSomething的,resetEvent);
resetEvents.Add(resetEvent);
}

//等待所有ManualResetEvents
WaitHandle.WaitAll的(resetEvents.ToArray()); //你可能想使用一个数组,而不是一个列表,列表是只为:-)
}

公共静态无效DoSomething的(对象数据)
的例子更容易{
ManualResetEvent的resetEvent =数据ManualResetEvent的;

//做些什么

resetEvent.Set();
}



编辑:忘了提,你可以等待一个线程,任何线程和等等为好。
也根据自己的情况,的AutoResetEvent这样事情就简单一点,因为它(顾名思义),可以自动信号事件: - )


Is there a better way to wait for queued threads before execute another process?

Currently I'm doing:

this.workerLocker = new object(); // Global variable
this.RunningWorkers = arrayStrings.Length; // Global variable

// Initiate process

foreach (string someString in arrayStrings)
{
     ThreadPool.QueueUserWorkItem(this.DoSomething, someString);
     Thread.Sleep(100);
}

// Waiting execution for all queued threads
lock (this.workerLocker)  // Global variable (object)
{
     while (this.RunningWorkers > 0)
     {
          Monitor.Wait(this.workerLocker);
     }
}

// Do anything else    
Console.WriteLine("END");


// Method DoSomething() definition
public void DoSomething(object data)
{
    // Do a slow process...
    .
    .
    .

    lock (this.workerLocker)
    {
        this.RunningWorkers--;
        Monitor.Pulse(this.workerLocker);
    }
}

解决方案

You likely want to take a look at AutoResetEvent and ManualResetEvent.

These are meant for exactly this situation (waiting for a ThreadPool thread to finish, prior to doing "something").

You'd do something like this:

static void Main(string[] args)
{
    List<ManualResetEvent> resetEvents = new List<ManualResetEvent>();
    foreach (var x in Enumerable.Range(1, WORKER_COUNT))
    {
    	ManualResetEvent resetEvent = new ManualResetEvent();
    	ThreadPool.QueueUserWorkItem(DoSomething, resetEvent);
        resetEvents.Add(resetEvent);
    }

    // wait for all ManualResetEvents
    WaitHandle.WaitAll(resetEvents.ToArray()); // You probably want to use an array instead of a List, a list was just easier for the example :-)
}

public static void DoSomething(object data)
{
    ManualResetEvent resetEvent = data as ManualResetEvent;

    // Do something

    resetEvent.Set();
}

Edit: Forgot to mention you can wait for a single thread, any thread and so forth as well. Also depending on your situation, AutoResetEvent can simplify things a bit, since it (as the name implies) can signal events automatically :-)

这篇关于有没有更好的方式来等待排队的主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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