Harsh Baid
Harsh Baid

Reputation: 7249

VersionNotFoundException on changing the DataRow in BackgroundWorkerProgressChanged event

This row["FileProgress"] = e.ProgressPercentage; code below is giving error of VersionNotFoundException. Please help because its creating havoc to the application, also I am facing Exception: DataTable internal index is corrupted: '5' many times randomly.

BackgroundWorker ProgressChanged code

private void bwTransferQueue_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
    DataRow row = e.UserState as DataRow;
    if (row != null)
    {
        row["FileProgress"] = e.ProgressPercentage;     <--- VersionNotFoundException
    }
}

BackgroundWorker DoWork code

private void bwTransferQueue_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    DataRow row = e.Argument as DataRow;
    try
    {
        if (row != null)
        {
             // some code
             bwTransferQueue.ReportProgress(0, row);
        }
    }
    catch (WebException webex)
    {
        row["Status"] = QueueType.Failed;
        row["StatusDescription"] = webex.Status;
        e.Result = row;
    }
    catch (Exception ex)
    {
        row["Status"] = QueueType.Failed;
        row["StatusDescription"] = ex.Message;
        e.Result = row;
    }
}

Code to Start Background Worker

private void startWorker()
{
    try
    {
         if (StartQueue && dtTransferQueue.Rows.Count > 0 && !bwTransferQueue.IsBusy)
         {
              DataRow[] rows = dtTransferQueue.Select(string.Format("Status = '{0}'", QueueType.Pending.ToString()));
              if (rows.Length > 0)
              {
                  tsmiProgressBar.Visible = true;
                  bwTransferQueue.RunWorkerAsync(rows[0]);
              }
         }
    }
    catch (Exception ex)
    {
          CommonLogic.HandleError(ex);
    }
}

Upvotes: 0

Views: 979

Answers (1)

Alex Humphrey
Alex Humphrey

Reputation: 6219

You are modifying the DataRow on more than one thread.

The DoWork event of BackgroundWorker is raised on a background thread. When you call ReportProgress, the ProgressChanged event is raised on the GUI thread. The reason for this is so that you can easily update UI elements from the ProgressChanged event handler.

This may be why you are corrupting the the DataTable index.

As for the VersionNotFoundException, I can imagine that it's related to the multithreaded access/corruption in some way, but don't know for sure. However, I'd definately start by looking at how your threads are interacting with the DataRow.

Upvotes: 0

Related Questions