使用BackgroundWorker时出现异常

时间:2016-04-18 13:27:00

标签: c# winforms backgroundworker

我正在尝试将一些数据填充到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();
}

Exception1:

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: 

Exception2:

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时我是否遗漏了某些内容。任何帮助都会非常感激。

此致

阿尼什。

1 个答案:

答案 0 :(得分:0)

在检查堆栈跟踪和所有内容时,看起来Populate()方法本身在ResumeComputation()方法之后调用了两次。

  

在Syncfusion.PivotAnalysis.Base.PivotEngine.PopulatePivotTable()中   d:\ Work_Vol4 \ SVN \工作室\干线\ work_area \ GridWF \工程师\ PivotAnalysis.Base的\ src \发动机\ PivotEngine.cs:行   5932在   Syncfusion.PivotAnalysis.Base.PivotEngine.ResumeComputations(布尔   resetPivotCollections,Boolean shouldRefresh)in   d:\ Work_Vol4 \ SVN \工作室\干线\ work_area \ GridWF \工程师\ PivotAnalysis.Base的\ src \发动机\ PivotEngine.cs:行   2735在   Syncfusion.PivotAnalysis.Base.PivotEngine.ResumeComputations(布尔   resetPivotCollections)中   d:\ Work_Vol4 \ SVN \工作室\干线\ work_area \ GridWF \工程师\ PivotAnalysis.Base的\ src \发动机\ PivotEngine.cs:行   2718在Syncfusion.PivotAnalysis.Base.PivotEngine.Populate()中   d:\ Work_Vol4 \ SVN \工作室\干线\ work_area \ GridWF \工程师\ PivotAnalysis.Base的\ src \发动机\ PivotEngine.cs:行   2829年   Syncfusion.Windows.Forms.PivotAnalysis.PivotGridControlBase.Populate(PivotEngine   PE,

您必须避免此类重复递归调用才能停止此异常。