我在for循环中调用此代码,我需要这样做,因为进度取决于for循环值。
bgworker1.ReportProgress(k * count);
但我收到一个例外:
this operation has already had operation completed called on it and further calls are illegal
我该如何解决这个问题?
编辑:
private void bgworker1_DoWork(object sender, DoWorkEventArgs e)
{
for (k = 1; k <= tcount; k++)
{
bgworker1.ReportProgress(k * count);
}
}
private void bgworker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void bgworker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
bgworker1.ReportProgress(k * count);
}
答案 0 :(得分:2)
您可以解决此问题的一种方法是报告此唯一任务的独占for循环中的进度,例如
private void bgworker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 100; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
//Insert your logic HERE
worker.ReportProgress(i * 1);
}
}
}