Missy Missy - 1 year ago 88
C# Question

Progress Bar with Label — Unable to Update Label in ProgressChanged

I have a background worker with a long running task. The task goes through a list of files and I want to update the user with which file we are on. I have a tool strip that has a label named panel1.text. The progress bar is working however the label is not changing in my ProgressChanged method i.e. It should say Processing File1 then change to Processing File2, but it stays on the default of Processing.

private void btnProcess_Click(object sender, EventArgs e)
toolStripProgressBar1.Visible = true;
toolStripProgressBar1.Maximum = 1000000000;
panel1.Text = "Processing "; // this appears properly
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(processFiles);
worker.ProgressChanged += ProgressChanged;
while (worker.IsBusy)
// the reason for this is because nothing can happen until the processing is done
// more processing

private void ProgressChanged(object sender, ProgressChangedEventArgs e)
panel1.Text = "Processing "+ e.UserState.ToString(); <<<---- This is Not Updating panel1.Text but it evaluates properly

private void processFiles(object sender, EventArgs e)
int retVal = 0;
foreach (string fileName in listBox1.Items)
ProgressChangedEventArgs ea = new ProgressChangedEventArgs(1,fileName);
ProgressChanged(this, ea);

// do more processing


I would appreciate any help.

Answer Source

You are using the same thread, which is being blocked by another process. You need to use a Task to create a new thread and possibly use Dispatcher.BeginIvoke if the control is on the other thread. Make sure whatever Button Click, etc is happening is marked with the Async keyword as well to make it Asynchronous.


Await Task mytask = Task.Run(() => 
                             for(var i = 0; i < 1000; i++)
                                Label.Dispatcher.BeginInvoke( () =>
                                                                 UpdateMe(int i, LabelClass/Component class/component)});

Then inside the Label Class or wherever the label is:

Public void UpdateMe(int i, LabelClass class)
  class.label.content = Cint((i/Total)*100);

There are other ways to do it as well such as Binding the value to the UI, but this will give you a better understanding of why its not working and how things work with other threads.

If you want to really get a visual understanding call:

 `Console.WriteLine($"Current Thread ID: System.Threading.Thread.CurrentThread.ManagedThreadId}");`

Right before you go into the Task---it will give you the main thread ID Then inside the Task call it again...this will give you the secondary thread ID.
Then Right before the Dispatcher call:

Console.WriteLine($"Do I have access to the label on this thread? {Label.Dispatcher.CheckAccess()}";

If you have access it will display True, if not it will display False...In your case it will display false because its owned by the other thread, but you can use the Dispatcher to be able to do work on that thread while in another thread...

Also, I recommend you not use Background Worker and use Tasks instead...this explains why in depth...basically Tasks do everything Background workers do and more, have less issues and are easier to work with...


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download