幾乎所有背景工作者的例子都是由for循環組成的。但我的要求不需要任何循環。我有以下代碼運行我backgroundworker。如何取消工作進程不使用for循環如何阻止背景工作者
void form_DoWork(LoadingProgress sender, DoWorkEventArgs e)
{
//for (int i = 0; i < 100; i++)
//{
// System.Threading.Thread.Sleep(50);
// sender.SetProgress(i, "Step " + i.ToString() + "/100...");
// if (sender.CancellationPending)
// {
// e.Cancel = true;
// return;
// }
//}
// heavy database process
SomeClass.BulkInsert(ExportLine);
}
private void ButtonCancelClick(object sender, EventArgs e)
{
//notify the background worker we want to cancel
worker.CancelAsync();
//disable the cancel button and change the status text
buttonCancel.Enabled = false;
labelStatus.Text = CancellingText;
}
你不能,至少不容易。由於BulkInsert沒有提供取消操作的方法,因此您可以儘量避免中止該線程,而這非常麻煩。 – 2012-03-21 04:53:52
任何替代方案,以顯示耗時處理的進度工作 – arjun 2012-03-21 05:01:04
男士們請任何幫助 – arjun 2012-03-21 05:47:20