我写了一些代码:的Parallel.For失败(C#)
class Program
{
public const int count = 3000;
static List<int> list = new List<int>();
static void DoWork(int i)
{
list.Add(i);
}
static void Main(string[] args)
{
while (true)
{
Stopwatch s = new Stopwatch();
s.Start();
Parallel.For(0, count + 1, DoWork);
s.Stop();
Console.WriteLine("\n Elapsed: " + s.Elapsed.ToString());
Console.WriteLine("Expected: {0}", count + 1);
Console.WriteLine("count: {0}", list.Count);
Console.ReadKey();
list = new List<int>();
}
}
}
但不预期的结果(
不Console.WriteLine调用
什么是之前所有周期的完成使用Parallel.For的问题?