How to update label text and pictureBox image from backgroundWorker_ProgressChanged?
- I have a backgroundWorker, and it works great, but I want to change the text of a label and the image in a pictureBox several times as the progress is changed. The progressBar works fine. I can't figure out how the best way to do this is though. I can change the text of the label, but it appears I can do this only once?
Any help is greatly appreciated. I have attached my code, but have not written code for changing the image of the pictureBox, as I figured this would be done along with the label and it appears my timing is off or something...
Code Snippetprivate void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ProgressBar1.Visible = true;
ProgressBar1.Value = e.ProgressPercentage;
if (Label2.Text == "Backing up Database...")
{
Label2.Text = "Backing up Resources (Images, etc.)...";return;
}
if (Label2.Text == "Backing up Resources (Images, etc.)...")
{
Label2.Text = "Database and Resources Backed Up Successfully!";return;
}
}
Thanks Again,
Answers
- Are you saying that the text "Database and Resources Backed Up Successfully!" is never displayed? Does the event get fired more than once? You should be able to change the PictureBox's image in a similar manner.
Hi MLyons10,
As far as I know, the ProgressChanged event was raised by calling ReportProgress method of BackgroundWorker class in the DoWork event hander, a model like below.
void InitializeBackgoundWorker()Code Snippetprivate}
{
backgroundWorker1.DoWork +=
new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(
backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.ProgressChanged +=
new ProgressChangedEventHandler(
backgroundWorker1_ProgressChanged);
}
private void backgroundWorker1_DoWork(object sender,
DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
// do the task
// calling ReportProgress when percentage was changed
Worker.ReportProgress(completed_percentage_of_task)
}
private void backgroundWorker1_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
// do something when the task was completed
}
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
// to show the progress of the task in some controls
In addition to the BackgroundWorker Class, please check the following LINK below.
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Regards,
Xun
MLyons10 wrote: Code Snippetprivate void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ProgressBar1.Visible = true;
ProgressBar1.Value = e.ProgressPercentage;
if (Label2.Text == "Backing up Database...")
{
Label2.Text = "Backing up Resources (Images, etc.)...";return;
}
if (Label2.Text == "Backing up Resources (Images, etc.)...")
{
Label2.Text = "Database and Resources Backed Up Successfully!";return;
}
}throw in a couple of Application.DoEvents(); everytime you actually want to display something.
Code Snippetprivate void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ProgressBar1.Visible = true;
ProgressBar1.Value = e.ProgressPercentage;
if (Label2.Text == "Backing up Database...")
{
Label2.Text = "Backing up Resources (Images, etc.)...";Application.DoEvents();
return;
}
if (Label2.Text == "Backing up Resources (Images, etc.)...")
{
Label2.Text = "Database and Resources Backed Up Successfully!";Application.DoEvents();
return;
}
}

