同为WaitAll线程问题,做活动 - 点火信号异常 [英] threading problem with WaitAll and done events - signal firing exception

查看:209
本文介绍了同为WaitAll线程问题,做活动 - 点火信号异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用线程池来(这并不奇怪)管理一组线程。我所试图做的是让他们对信号做的时候,我有:

I'm using the threadpool to (not surprisingly) manage a set of threads. What I am trying to do is get them to signal when done, I have:

ManualResetEvent[] doneEvents = new ManualResetEvent[char_set.Length];

public struct string_char
    {
        public string[] _str_char;
        public ManualResetEvent _doneEvent;

        public string_char(string[] str_char, ManualResetEvent doneEvent)
        {
            _str_char = str_char;
            _doneEvent = doneEvent;
        }
    }



我这里有一个循环,创建一个字符数组然后我创建我的结构填充字符数组和做活动的一个实例:

I have a loop here that creates an array of char and then I create an instance of my struct populating the char array and a done event:

doneEvents[no_of_elements - 1] = new ManualResetEvent(false);
            string_char s_c = new string_char(array_holder, doneEvents[no_of_elements - 1]);
            ThreadPool.QueueUserWorkItem(ThreadPoolCallback, s_c);



因此,线程被创建,添加到池中,它欢快地熄灭并运行时,它完成它设置完成事件:

So, the thread is created, added to the pool, and it goes off merrily and runs, when it completes it sets the done event:

public void ThreadPoolCallback(Object s_c)
{
string_char _s_c = (string_char)s_c;
//do the calculations...
//when done:
_s_c._doneEvent.Set();
}



回到主循环代码在这里等着:

Back at the main loop the code is waiting here:

WaitHandle.WaitAll(doneEvents);
Console.WriteLine("All calculations are complete.");



麻烦的是我不断收到异常:

The trouble is I keep getting the exception:

'WaitAll for multiple handles on a STA thread is not supported.'

我已经看过这在谷歌,但它并没有真正的帮助,我在做什么错。这基本上是毫秒MSDN例子重演,除了我使用的是结构,而不是一类?

I have looked this up on google but it doesn't really help, what am I doing wrong. This is basically a repeat of the ms msdn example, apart from I am using a struct rather than a class?

我解决了这一问题,使用下面的建议,通过切换到MTA(DOH!)主;还了解到,最大为64线程数是很有用的。因此,我不得不切换到不同的等待模型作为最终的应用程序将运行比多几个!很多要学。

I fixed the problem, using the advice below, by switching to MTA (doh!) in the main; also it was useful to learn that the max is 64 thread count. So I am going to have to switch to a different wait model as the final app will be running a few more than that! Lots to learn.

感谢。

推荐答案

有一对夫妇用这种方式使用 WaitHandle.WaitAll的的问题。你已经发现了其中的一个。另一种是,它不是非常可伸缩,因为它有一个64句柄限制。下面是我用它来等待多个工作项目来完成的模式。它使用 CountdownEvent 类。

There are a couple of issues with using WaitHandle.WaitAll in this manner. You have already discovered one of them. The other is that it is not very scalable since it has a 64 handle limit. Here is the pattern I use to wait for multiple work items to complete. It uses the CountdownEvent class.

using (var finished = new CountdownEvent(1))
{
  foreach (var workItem in workItemCollection)
  {  
     var captured = item;
     finished.AddCount(); 
     ThreadPool.QueueUserWorkItem(
       (state) =>
       {
         try
         {
           ProcessWorkItem(captured);
         }
         finally
         {
           finished.Signal();
         }
       }, null);
  } 
  finished.Signal();
  finished.Wait();
}

这篇关于同为WaitAll线程问题,做活动 - 点火信号异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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