0

下面的row["FileProgress"] = e.ProgressPercentage;代码给出了错误VersionNotFoundException。请帮助,因为它对应用程序造成严重破坏,我也面临异常:DataTable 内部索引已损坏:随机多次出现“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);
    }
}
4

1 回答 1

0

您正在修改DataRow多个线程。

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

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

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.

于 2011-08-10T11:34:06.780 回答