关于在BackgroundWorkerProgressChanged事件中更改DataRow的VersionNotFoundException

时间:2011-08-10 10:44:11

标签: c# .net datagridview windows-applications krypton-toolkit

以下row["FileProgress"] = e.ProgressPercentage;代码的错误为VersionNotFoundException。请帮助,因为它会对应用程序造成严重破坏,我也会随机多次遇到异常:DataTable internal index is corrupted: '5'

BackgroundWorker ProgressChanged代码

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代码

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;
    }
}

启动后台工作程序的代码

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);
    }
}

1 个答案:

答案 0 :(得分:0)

您正在修改多个线程上的DataRow

DoWork BackgroundWorker事件在后台线程上引发。当您调用ReportProgress时,会在GUI线程上引发ProgressChanged事件。这样做的原因是您可以从ProgressChanged事件处理程序轻松更新UI元素。

这可能是您破坏DataTable索引的原因。

至于VersionNotFoundException,我可以想象它在某种程度上与多线程访问/损坏有关,但不确定。但是,我肯定会先看看你的主题如何与DataRow进行交互。