Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

c# - WaitAll for multiple handles on a STA thread is not supported

  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!

Note: It error is at line WaitHandle.WaitAll(doneEvents); I'm using a standard WPF project.

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!");
}

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:

Error: "WaitAll for multiple handles on a STA thread is not supported."

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually I use the following to replace WaitHandle.WaitAll(doneEvents);

foreach (var e in doneEvents)
    e.WaitOne();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...