我是使用 Parallel 的新手,所以我可能遗漏了一些东西。使用运行此代码时,Parallel.For
我得到一个 NullReferenceExcempion。当用 normal 做完全相同的事情时,它可以完美地工作。我发现其他人也问过同样的问题,但在他们的情况下,问题始终是他们在 for 范围之外声明变量,这当然会产生各种混乱的事情。我发现没有人遇到这个问题,因为我正在经历它。在我的情况下,变量是在内部声明的......我怎样才能获得空引用?
Parallel.For(0, Outer.ThisValidation.RawData.RawDataStorage.Count, i =>
{
//This line gets the NullReference.
//I'm declaring and assigning the variable in the same line, how on Earth can ThisSer throw a NullReferenceException
Series ThisSer = chartEquitiesBySymbol.Series.Add(Outer.ThisValidation.RawData.RawDataStorage[i].StringedIndex);
ThisSer.IsVisibleInLegend = false;
ThisSer.ChartType = SeriesChartType.Line;
ThisSer.BorderWidth = 2;
for (int j = 0; j < Outer.ThisValidation.RawData.RawDataStorage[i].EquityData.EquityLine.Count; j++)
ThisSer.Points.AddXY(Outer.ThisValidation.RawData.RawDataStorage[i].EquityData.EquityLine[j].Date, Outer.ThisValidation.RawData.RawDataStorage[i].EquityData.EquityLine[j].Value);
Outer.ThisValidation.RawData.RawDataStorage[i].AddSeries(WindowID, ThisSer);
});
错误显示在评论中。我正在向图表中添加一个系列,该系列的引用会立即存储在一个对象中,以便以后能够使用它来做我的事情。我在分配阶段得到空引用异常。通常它会在 5 到 15 次之间的可变迭代次数后阻塞。
这是发生的事情:
带有红色 X 的行正在引发异常。工具提示在分配阶段显示空值。