Exception while using BackgroundWorker
<p>I am trying to populate some data to my custom grid in the BackgroundWorker. If the data is small then it is working fine but if the data is large, I am getting exceptions randomly. Sometimes I am getting “NullReferenceException was unhandled by user code” and sometimes I am getting “ArgumentOutOfRangeException was unhandled by user code”. Below I have mentioned some of the exceptions I am facing. </p>

<p>Basically what I am doing in Populate() method is that by using foreach loop I am assigning the values of each cell in my custom grid. This works fine when I am not using the BackgroundWorker. But while using the BackgroundWorker I am getting null values while assigning it to the cell. Hence lot of exceptions arises. I don’t know why this is happening. Is there any suggestions to overcome this?</p>

<p>Below is the code that I am using</p>

<p><strong>Code:</strong></p>

<pre><code>BackgroundWorker worker = new BackgroundWorker();

worker.DoWork += (sender, args) =&gt;
{
if (this.PivotEngine.DataSource != null &amp;&amp; this.PivotCalculations.Count &gt; 0)
{

    // Populates the data for the grid. 
    // Without using the BackgroundWorker, the values are been populated properly.
    this.PivotEngine.Populate();        

}

};

worker.RunWorkerCompleted += (s, e) =&gt;
{
MessageBox.Show("Process is complete");
};

if (!worker.IsBusy)
{
  worker.RunWorkerAsync();
}
</code></pre>

<p><strong>Exception1:</strong></p>

<pre><code>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.&lt;&gt;c__DisplayClass1d.&lt;ApplyRowCols&gt;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: 
</code></pre>

<p><strong>Exception2:</strong></p>

<pre><code>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.&lt;&gt;c__DisplayClass1d.&lt;ApplyRowCols&gt;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:
</code></pre>

<p>Am I missing something while using the BackgroundWorker. Any help would be muchly appreciated.</p>

<p>Regards,</p>

<p>Anish.</p>