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.