Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6084987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:35:15+00:00 2026-05-23T11:35:15+00:00

I’m using the threadpool to (not surprisingly) manage a set of threads. What I

  • 0

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.'

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?

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.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:35:16+00:00Added an answer on May 23, 2026 at 11:35 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.