不支持为WaitAll用于在STA线程多个句柄 [英] WaitAll for multiple handles on a STA thread is not supported

查看:1254
本文介绍了不支持为WaitAll用于在STA线程多个句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 为什么我得到这个错误信息? 为WaitAll用于在STA线程,不支持多个句柄。

  2. 我应该用[MTAThreadAttribute] attribut? 更新:不到风度与WPF应用程序工作

  1. Why do I get this error message? "WaitAll for multiple handles on a STA thread is not supported."
  2. Should I use [MTAThreadAttribute] attribut? Update: Dosn't work with WPF applications!

请注意:
 这错误是在行WaitHandle.WaitAll的(doneEvents);
 我使用的是标准的 WPF项目

private void Search()
{
    const int CPUs = 2;
    var doneEvents = new ManualResetEvent[CPUs];

    // Configure and launch threads using ThreadPool:
    for (int i = 0; i < CPUs; i++)
    {
        doneEvents[i] = new ManualResetEvent(false);
        var f = new Indexer(Paths[i], doneEvents[i]);
        ThreadPool.QueueUserWorkItem(f.WaitCallBack, i);
    }

    // Wait for all threads in pool 
    WaitHandle.WaitAll(doneEvents);
    Debug.WriteLine("Search completed!");
}

更新:以下解决方案不适用于WPF应用程序工作!
这是不可能改变的主要应用属性MTAThreadAttribute。这将导致以下错误:

Update: The following solution doesn’t work for WPF applications! It is not possible to change the main application attribute to MTAThreadAttribute. It will result in the following error:

错误:

推荐答案

怎么样使用任务做你的线程你。

What about using the Tasks to do your threading for you.

<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx\">http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx

var task1 = Task.Factory.StartNew(() => DoSomeWork());
var task2 = Task.Factory.StartNew(() => DoSomeWork());
var task3 = Task.Factory.StartNew(() => DoSomeWork());
Task.WaitAll(task1, task2, task3);

这篇关于不支持为WaitAll用于在STA线程多个句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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