Carl Daniel said:
Tom said:
Carl Daniel [VC++ MVP] wrote:
Tom Spink wrote:
What you'll need to do is create an AutoResetEvent for each thread,
and when a thread completes, call the 'Set' method on the
AutoResetEvent. AutoResetEvent inherits from WaitHandle, so all you
do is pass an array of the AutoResetEvent's into the WaitAll method,
and when each and every AutoResetEvent is signalled (i.e. Set has
been called) the thread calling WaitAll will resume.
No, you don't need to do that - you can wait directly on the Thread
itself. For the .NET Thread object, waiting for the thread to
complete is done by
calling the Join member function. Internally that'll end up calling
WaitForSingleObject on the thread handle itself, which becomes
signalled when the thread terminates.
-cd
This is true, but then you'd need to call 'Join' sequentially on each
running worker thread.
Yep - which accomplishes the sames thing as WaitAll on a bunch of Event
handles much more efficiently, with fewer opportunities for error and
with fewer resources (since you don't need events).
-cd
I am getting close... I did get the join to work but this does not seem to
be the best way to go. In addition, I am still unclear as to how to start
an aribitrary number of threads.
I am trying to do something like:
private void btnSubmit_Click(object sender, EventArgs e)
{
int iNoThreads = Convert.ToInt16(tbNoThreads.Text)
for (int i=0; i<iNoThreads; i++)
{
// create the test process thread
}
// wait for all test "SubmitOrders" process threads to complete
// display elapsed time and other metrics
}
private void SubmitOrders()
{
// random order submits
}
So far, I have gotten the code below to work, but it does not allow for
the creation of an arbitrary number of threads.
private void btnSubmit_Click(object sender, EventArgs e)
{
int iNoTxns = Convert.ToInt16(tbNoTxns.Text);
tbElapsedTime.Text = null;
tbElapsedTime.Refresh();
DateTime startTime = DateTime.Now;
ThreadStart entrypoint = new ThreadStart(SubmitOrders);
Thread thread = new Thread(entrypoint);
thread.Name = "SubmitOrders01";
thread.Start();
thread.Join();
DateTime stopTime = DateTime.Now;
TimeSpan duration = stopTime - startTime;
tbElapsedTime.Text = duration.Hours + ":" + duration.Minutes + ":" +
duration.Seconds + ":" + duration.Milliseconds;
}