I have some code that looks like this:
int[] customerIds = { 1, 2, 3, 4, 5, 6, 7, 8 };
List<Threadthreads = new List<Threads>();
foreach (int customerId in customerIds)
{
Thread thread = new Thread(delegate()
{
// do calculation with customerId
});
threads.Add(thread);
thread.Start();
}
foreach (Thread thread in threads)
{
thread.Join();
}
First, when I run this code, it appears as though 8 is executed
multiple times. My guess is that the thread continues on after the
delegate finishes, and since MoveNext() on the List<intjust returns
the last item over an over, we see 8 multiple times. Is that accurate?
Furthermore, something seems wrong about storing threads in a
List<Threadand then joining. Is there a better way to Join these
theads without keeping a reference to them around?
Thanks,
Travis