After the background worker is done doing its process, I want to change the text on certain labels on the form.
Here’s the Button that fires the backgroundworker:
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();
}
}
Here’s the method that changes text for the labels on the form:
private void ResetDirectoryStatistics()
{
lblSelectedDirectory.Text = "N/A";
lblTotalNumberOfFilesInDirectory.Text = "N/A";
lblTotalNumberOfSupportedFilesInDirectory.Text = "N/A";
lblTotalNumberOfUnsupportedFilesInDirectory.Text = "N/A";
lblTotalNumberOfPoliciesInDirectory.Text = "N/A";
}
When dealing with the background worker, where should I place the ResetDirectoryStatistics method? I cannot place it in the “DoWork” method of the backgroundworker because that would be cross-threading. And if I place the method right after processImagesWorker.RunWorkerAsync();, it’ll execute itself before RunWorker is complete.
You should call your method in the
RunWorkerCompletedevent of the background worker. This event uses the UI thread, so no cross-threading issue to fear.