我正在尝试将一些数据填充到BackgroundWorker中的自定义网格中。如果数据较小,则工作正常,但如果数据较大,则随机出现异常。有时我收到“用户代码未处理NullReferenceException”,有时我收到“用户代码未处理ArgumentOutOfRangeException”。下面我提到了我所面临的一些例外。
基本上,我在Populate()方法中所做的就是通过使用foreach循环来分配自定义网格中每个单元格的值。当我不使用BackgroundWorker时,这可以正常工作。但是在使用BackgroundWorker时,将其分配给单元格时会得到空值。因此,出现了许多例外情况。我不知道为什么会这样。有什么建议可以克服吗?
下面是我正在使用的代码
码:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (sender, args) =>
{
if (this.PivotEngine.DataSource != null && this.PivotCalculations.Count > 0)
{
// Populates the data for the grid.
// Without using the BackgroundWorker, the values are been populated properly.
this.PivotEngine.Populate();
}
};
worker.RunWorkerCompleted += (s, e) =>
{
MessageBox.Show("Process is complete");
};
if (!worker.IsBusy)
{
worker.RunWorkerAsync();
}
例外1:
Exception:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Syncfusion.PivotAnalysis.Base
StackTrace:
at Syncfusion.PivotAnalysis.Base.PivotEngine.PopulatePivotTable() in d:\Work_Vol4\svn\studio\trunk\work_area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:line 5882
at Syncfusion.PivotAnalysis.Base.PivotEngine.ResumeComputations(Boolean resetPivotCollections, Boolean shouldRefresh) in d:\Work_Vol4\svn\studio\trunk\work_area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:line 2735
at Syncfusion.PivotAnalysis.Base.PivotEngine.ResumeComputations(Boolean resetPivotCollections) in d:\Work_Vol4\svn\studio\trunk\work_area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:line 2718
at Syncfusion.PivotAnalysis.Base.PivotEngine.Populate() in d:\Work_Vol4\svn\studio\trunk\work_area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:line 2829
at Syncfusion.Windows.Forms.PivotAnalysis.PivotGridControlBase.Populate(PivotEngine pe, Boolean checkLazyLoading) in d:\Work_Vol4\svn\studio\trunk\work_area\GridWF\Engineer\PivotAnalysis.Windows\Src\PivotGridControl\Core\PivotGridControlBase.cs:line 2990
at Syncfusion.Windows.Forms.PivotAnalysis.PivotGridControlBase.<>c__DisplayClass1d.<ApplyRowCols>b__1a(Object sender, DoWorkEventArgs args) in d:\Work_Vol4\svn\studio\trunk\work_area\GridWF\Engineer\PivotAnalysis.Windows\Src\PivotGridControl\Core\PivotGridControlBase.cs:line 3016
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
InnerException:
例外2:
System.ArgumentOutOfRangeException was unhandled by user code
HResult=-2146233086
Message=Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source=mscorlib
ParamName=index
StackTrace:
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.Collections.Generic.List`1.get_Item(Int32 index)
at Syncfusion.PivotAnalysis.Base.PivotEngine.PopulatePivotTable() in d:\Work_Vol4\svn\studio\trunk\work_area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:line 5932
at Syncfusion.PivotAnalysis.Base.PivotEngine.ResumeComputations(Boolean resetPivotCollections, Boolean shouldRefresh) in d:\Work_Vol4\svn\studio\trunk\work_area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:line 2735
at Syncfusion.PivotAnalysis.Base.PivotEngine.ResumeComputations(Boolean resetPivotCollections) in d:\Work_Vol4\svn\studio\trunk\work_area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:line 2718
at Syncfusion.PivotAnalysis.Base.PivotEngine.Populate() in d:\Work_Vol4\svn\studio\trunk\work_area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:line 2829
at Syncfusion.Windows.Forms.PivotAnalysis.PivotGridControlBase.Populate(PivotEngine pe, Boolean checkLazyLoading) in d:\Work_Vol4\svn\studio\trunk\work_area\GridWF\Engineer\PivotAnalysis.Windows\Src\PivotGridControl\Core\PivotGridControlBase.cs:line 2990
at Syncfusion.Windows.Forms.PivotAnalysis.PivotGridControlBase.<>c__DisplayClass1d.<ApplyRowCols>b__1a(Object sender, DoWorkEventArgs args) in d:\Work_Vol4\svn\studio\trunk\work_area\GridWF\Engineer\PivotAnalysis.Windows\Src\PivotGridControl\Core\PivotGridControlBase.cs:line 3016
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
InnerException:
使用BackgroundWorker时我是否缺少某些东西?任何帮助将不胜感激。
问候,
阿尼什
最佳答案
在检查堆栈跟踪和所有内容时,似乎Populate()方法本身在ResumeComputation()
方法之后被调用了两次。
在Syncfusion.PivotAnalysis.Base.PivotEngine.PopulatePivotTable()中
d:\ Work_Vol4 \ svn \ studio \ trunk \ work_area \ GridWF \ Engineer \ PivotAnalysis.Base \ Src \ Engine \ PivotEngine.cs:line
5932在
Syncfusion.PivotAnalysis.Base.PivotEngine.ResumeComputations(Boolean
resetPivotCollections,布尔值应该刷新)
d:\ Work_Vol4 \ svn \ studio \ trunk \ work_area \ GridWF \ Engineer \ PivotAnalysis.Base \ Src \ Engine \ PivotEngine.cs:line
2735年
Syncfusion.PivotAnalysis.Base.PivotEngine.ResumeComputations(Boolean
resetPivotCollections)中
d:\ Work_Vol4 \ svn \ studio \ trunk \ work_area \ GridWF \ Engineer \ PivotAnalysis.Base \ Src \ Engine \ PivotEngine.cs:line
2718在Syncfusion.PivotAnalysis.Base.PivotEngine.Populate()中
d:\ Work_Vol4 \ svn \ studio \ trunk \ work_area \ GridWF \ Engineer \ PivotAnalysis.Base \ Src \ Engine \ PivotEngine.cs:line
2829年
Syncfusion.Windows.Forms.PivotAnalysis.PivotGridControlBase.Populate(PivotEngine
pe
您必须避免这种重复的递归调用才能停止此异常。