Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用BackgroundWorker时发生异常_C#_Winforms_Backgroundworker - Fatal编程技术网

C# 使用BackgroundWorker时发生异常

C# 使用BackgroundWorker时发生异常,c#,winforms,backgroundworker,C#,Winforms,Backgroundworker,我正在尝试将一些数据填充到BackgroundWorker中的自定义网格中。如果数据很小,那么它可以正常工作,但是如果数据很大,我会随机得到异常。有时我得到“NullReferenceException未经用户代码处理”,有时我得到“ArgumentOutOfRangeException未经用户代码处理”。下面我提到了我面临的一些例外情况 基本上,我在Populate()方法中所做的是,通过使用foreach循环,我分配自定义网格中每个单元格的值。当我不使用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();
}
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: 
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:
例外情况1:

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:
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: 
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:
异常:
用户代码未处理System.NullReferenceException
HResult=-2147467261
Message=对象引用未设置为对象的实例。
Source=Syncfusion.PivotAnalysis.Base
堆栈跟踪:
在d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs中的Syncfusion.PivotAnalysis.Base.PivotEngine.PopulatePivotTable()处:第5882行
在Syncfusion.PivotAnalysis.Base.PivotEngine.Resumes(布尔重置PivotCollections,Boolean shouldRefresh)中的d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:第2735行
在Syncfusion.PivotAnalysis.Base.PivotEngine.Resumesculations(布尔重置PivotCollections)中的d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:第2718行
在d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs中的Syncfusion.PivotAnalysis.Base.PivotEngine.Populate()处:第2829行
在Syncfusion.Windows.Forms.PivotAnalysis.PivotGridControlBase.Populate(PivotEngine pe,Boolean checkLazyLoading)中的d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Windows\Src\PivotGridControl\Core\PivotGridControlBase.cs:第2990行
在Syncfusion.Windows.Forms.PivotAnalysis.PivotGridControlBase.c__中,在d:\Work\U Vol4\svn\studio\trunk\Work\area\GridWF\Engineer\PivotAnalysis.Windows\Src\PivotGridControl\Core\PivotGridControlBase.cs:第3016行中显示ClassAD.b_1a(对象发送方,DoWorkEventArgs args参数)
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventTargets e)
位于System.ComponentModel.BackgroundWorker.WorkerThreadStart(对象参数)
内部异常:
例外情况2:

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:
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: 
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:
System.ArgumentOutOfRangeException未由用户代码处理
HResult=-2146233086
Message=索引超出范围。必须为非负数且小于集合的大小。
参数名称:索引
Source=mscorlib
参数名=索引
堆栈跟踪:
在System.ThrowHelper.ThrowarGumentofrangeException处(ExceptionArgument参数,ExceptionResource资源)
位于System.Collections.Generic.List`1.get_项(Int32索引)
在d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs中的Syncfusion.PivotAnalysis.Base.PivotEngine.PopulatePivotTable()处:第5932行
在Syncfusion.PivotAnalysis.Base.PivotEngine.Resumes(布尔重置PivotCollections,Boolean shouldRefresh)中的d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:第2735行
在Syncfusion.PivotAnalysis.Base.PivotEngine.Resumesculations(布尔重置PivotCollections)中的d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:第2718行
在d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs中的Syncfusion.PivotAnalysis.Base.PivotEngine.Populate()处:第2829行
在Syncfusion.Windows.Forms.PivotAnalysis.PivotGridControlBase.Populate(PivotEngine pe,Boolean checkLazyLoading)中的d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Windows\Src\PivotGridControl\Core\PivotGridControlBase.cs:第2990行
在Syncfusion.Windows.Forms.PivotAnalysis.PivotGridControlBase.c__中,在d:\Work\U Vol4\svn\studio\trunk\Work\area\GridWF\Engineer\PivotAnalysis.Windows\Src\PivotGridControl\Core\PivotGridControlBase.cs:第3016行中显示ClassAD.b_1a(对象发送方,DoWorkEventArgs args参数)
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventTargets e)
位于System.ComponentModel.BackgroundWorker.WorkerThreadStart(对象参数)
内部异常:
我在使用BackgroundWorker时是否遗漏了什么。任何帮助都将不胜感激

问候,


Anish。

在检查堆栈跟踪和all时,似乎在
ResumeCompulation()方法之后调用了两次Populate()方法本身

在中的Syncfusion.PivotAnalysis.Base.PivotEngine.PopulatePivotTable()处 d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:line 5932 at Syncfusion.PivotAnalysis.Base.PivotEngine.ResumeComputions(布尔值) 重置数据透视集合,布尔值应为(刷新) d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:line 2735在 Syncfusion.PivotAnalysis.Base.PivotEngine.ResumeComputions(布尔值) 在中重置数据透视(集合) d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:line 2718在中的Syncfusion.PivotAnalysis.Base.PivotEngine.Populate()处 d:\Work\u Vol4\svn\studio\trunk\Work\u area\GridWF\Engineer\PivotAnalysis.Base\Src\Engine\PivotEngine.cs:line 2829在 Syncfusion.Windows.Forms.PivotAnalysis.PivotGridControlBase.Populate(数据透视引擎 体育


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

检查OnDoWor中的this.PivotEngine是否为空