后台工作人员完成其进程后,我想更改表单上某些标签上的文本。
这是触发 backgroundworker 的 Button:
private void btnProcessImages_Click(object sender, EventArgs e)
{
DialogResult processImagesWarnMsg = MessageBox.Show("You're about to process images, are you sure?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
if (processImagesWarnMsg == DialogResult.Yes)
{
DisableAllButtons();
if (!processImagesWorker.IsBusy)
{
processImagesWorker.RunWorkerAsync();
}
//The problem here is that the below will run BEFORE the worker is complete. Where should I place the below method in my code?
//ResetDirectoryStatistics();
}
}
这是更改表单上标签文本的方法:
private void ResetDirectoryStatistics()
{
lblSelectedDirectory.Text = "N/A";
lblTotalNumberOfFilesInDirectory.Text = "N/A";
lblTotalNumberOfSupportedFilesInDirectory.Text = "N/A";
lblTotalNumberOfUnsupportedFilesInDirectory.Text = "N/A";
lblTotalNumberOfPoliciesInDirectory.Text = "N/A";
}
与后台工作人员打交道时,我应该在哪里放置 ResetDirectoryStatistics 方法?我不能将它放在后台工作者的“DoWork”方法中,因为那将是跨线程的。如果我将方法放在 processImagesWorker.RunWorkerAsync(); 之后,它会在 RunWorker 完成之前自行执行。