我正在将一个excel文件上传到数据库。步骤如下: 1.我在excelRawData(DataTable)中有excel数据 2. UploadProgress是ProgressBar
int insertWkSuccess = 0;
bool wait = true;
int progress = 0;
uploadProgress.Visible = lblPercent.Visible = true;
btnCancel.Text = "Cancel Upload";
uploadProgress.Value = 0;
uploadProgress.Maximum = excelRawData.Rows.Count;
_workerUpload = null;
_workerUpload = new BackgroundWorker();
rtxtError.AppendText("Uploading Sheet Data.........\n");
_workerUpload.DoWork += (obj, args) =>
{
foreach (DataRow row in excelRawData.Rows)
{
if (wait)
{
if (_workerUpload.CancellationPending)
{
args.Cancel = true;
args.Result = null;
return;
}
if (upload.Upload(row, excelRawData.Rows.IndexOf(row) + 1, country, company, version, ref errorMsg))
insertWkSuccess++;
progress++;
if (_workerUpload == null)
{
break;
}
_workerUpload.ReportProgress(progress);
setPercentValue((progress*100)/uploadProgress.Maximum);
}
if (errorMsg.Count() == 0 && insertWkSuccess > 0)
{
upload.insertWktoMain(version, country, ref errorMsg);
}
args.Result = new object[] { insertWkSuccess, company, selectedSheet };
}
};
_workerUpload.ProgressChanged+=_workerUpload_ProgressChanged;
_workerUpload.RunWorkerCompleted+=_workerUpload_RunWorkerCompleted;
_workerUpload.WorkerReportsProgress = true;
_workerUpload.WorkerSupportsCancellation = true;
_workerUpload.RunWorkerAsync();
下面是我的“取消编码”按钮单击。
if (_workerUpload.IsBusy)
{
wait = false;
var confirmResult = MessageBox.Show("Are you sure you want to terminate the current upload process.",
"Confirm Stop Process!!",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (confirmResult == System.Windows.Forms.DialogResult.Yes)
{
wait = true;
_workerUpload.CancelAsync();
}
else
wait = true;
}
这里的问题是,当我等待“取消确认对话框”结果时,上传过程仍在后台继续。有没有一种方法可以停止后台进程,直到获得对话框的结果。
先谢谢了。