int a = 0;
System.Threading.Tasks.Parallel.For(0, 100000, (i) =>
{
a++;
});
Console.Write(a);
解決方案如下
int a = 0;
object lockObj= new object();
System.Threading.Tasks.Parallel.For(0, 100000, (i) =>
{
lock (lockObj)
{
a++;
}
});
Console.Write(a);
結果就正常了.