我有一个简单的表单,上面有 2 个进度条和 1 个后台工作人员。我有 2 个循环(一个在另一个循环中),一旦增加,我想报告每个循环的进度。这是我的代码:
private void buttonStart_Click(object sender, EventArgs e)
{
workerCustomers.RunWorkerAsync();
}
private void workerCustomers_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
progressBar2.Value = (int)e.UserState;
}
private void workerCustomers_DoWork(object sender, DoWorkEventArgs e)
{
for (int customer = 0; customer < 50; customer++)
{
int customerPercentage = ++customer * 100 / 50;
workerCustomers.ReportProgress(customerPercentage, 0);
for (int location = 0; location < 500; location++)
{
int locationPercentage = ++location * 100 / 500;
workerCustomers.ReportProgress(customerPercentage, locationPercentage);
}
workerCustomers.ReportProgress(customerPercentage, 0);
}
}
当程序运行时,progressbar1 会更新得很好,但progressbar2 永远不会移动。如果我通过调试器运行它,我可以看到progressbar2 的值正在改变,只是图形上没有变化。有任何想法吗?