I found several examples that shows how to update UI control using BackgroundWorker.
For example the following code:
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
//start the operation on another thread
private void btnStart_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
//DoWork event handler is executed on a separate thread
private void backgroundWorker1_DoWork(object sender, DoWorkeventArgs e)
{
//a long running operation
for (int i = 1; i < 11; i++)
{
Thread.Sleep(2000);
backgroundWorker1.ReportProgress(i * 10);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.value = e.ProgressPercentage;
}
That I found here:
https://www.c-sharpcorner.com/UploadFile/Ashush/using-the-backgroundworker-component
The problem is that ALL the examples that I found, shows how to update Only One control on the form. For example the code that I gave, demonstrate how to update a progressBar value.
But this is not a realistic situation, in the real world, I would usually want to update SEVERAL controls on my form.
For example:
progressBar1.Value = 54;
listBox1.SelectedIndex = 2;
button1.BackgroundColor = Color.Yellow;
image1.left = image1.left + 20;
How do I change the code that I showed, in order to update the controls that I gave in the previous lines? There must be an easy way to do that. By the way, my application uses several threads, and each one of them have to update several controls on the UI while they are still running...
I Hope that my question is clear, Thanks.
Edit:
Neil suggested me to find the solution in this thread:
How do I update the GUI from another thread?
But I saw that people there recommended using BackgroundWorker... which is exactly what I'm asking about.
They didn't explain how to use this method in order to update several UI controls in parallel.