c# - 使用 BackgroundWorker 时出现异常

标签 c# winforms backgroundworker

我正在尝试将一些数据填充到我在 BackgroundWorker 中的自定义网格。如果数据很小,那么它工作正常,但如果数据很大,我会随机出现异常。有时我得到“NullReferenceException 未被用户代码处理”,有时我得到“ArgumentOutOfRangeException 未被用户代码处理”。下面我提到了我面临的一些异常(exception)情况。

基本上,我在 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();
}

异常(exception) 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: 

异常(exception) 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() 方法之后调用了两次。

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,

你必须避免这种重复的递归调用来停止这个异常。

关于c# - 使用 BackgroundWorker 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36695381/

相关文章:

c# - 在 iOS 中的 Xamarin Forms 中创建 HttpClient

c# - 为什么使用 EF/Linq to sql 创建性能不佳的查询如此容易

c# - 高级文件读取

c# - 如何在 .net 中有条件地继承类

c# - WebApi OAuth UseOAuthBearerAuthentication 给出 "Sequence contains more than one element"错误

c# - 从数据 GridView 中动态删除列

c# - Backgroundworker - 数据绑定(bind) - 查看更新

c# - 后台 worker ReportProgress 方法

c# - 如何避免通过winform向表中输入重复值?

c# - 如何防止在 ListView 中移动列?