我在让后台工作人员更新我的进度条时遇到了一些麻烦。我以在线教程为例,但我的代码不起作用。我在此站点上进行了一些挖掘,但找不到任何解决方案。我是后台工作人员/进度的新手。所以我不完全理解。
只是为了设置:我有一个主窗体(FORM 1),它打开另一个(FORM 3),带有进度条和状态标签。
我的表格 3 代码如下:
public string Message
{
set { lblMessage.Text = value; }
}
public int ProgressValue
{
set { progressBar1.Value = value; }
}
public Form3()
{
InitializeComponent();
}
我的表格 1 部分代码:
private void btnImport_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
if (MessageBox.Show("Are you sure you want to import " + cbTableNames.SelectedValue.ToString().TrimEnd('$') + " into " + _db, "Confirm to Import", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
alert = new Form3(); //Created at beginning
alert.Show();
backgroundWorker1.RunWorkerAsync();
}
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
int count = 0
foreach(DataRow row in DatatableData.Rows)
{
/*... Do Stuff ... */
count++;
double formula = count / _totalRecords;
int percent = Convert.ToInt32(Math.Floor(formula)) * 10;
worker.ReportProgress(percent, string.Format("Completed record {0} out of " + _totalRecords, count));
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
alert.Message = (String) e.UserState;
alert.ProgressValue = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
alert.Close();
}
所以。问题是它没有更新任何东西。进度条和标签都在更新。有人可以指出我的写作方向或有建议吗?