I have one method to handle a List<string>
parallel. Unfortunately I can't use .NET 4+.
But when I run this method, i
is always items.Count
public static void ParallelForEachTest(List<string> items)
{
if (items != null && items.Count > 0)
{
List<ManualResetEvent> mEventList = new List<ManualResetEvent>();
for (int i = 0; i < items.Count ; i++)
{
ManualResetEvent mEvent = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem((y) =>
{
Console.WriteLine(items[i] + i);
mEvent.Set();
});
mEventList.Add(mEvent);
}
mEventList.ForEach(x => x.WaitOne());
}
}
What do I need to change to achieve
x 0
x 1
x 2
for ParallelForEachTest(new List<string>(){"x","x","x"});