Alex Dunlop

Alex Dunlop

  • NA
  • 13
  • 893

How to modify UI from an another thread?

Feb 11 2021 7:39 PM
Hi friends,
For testing and learning purposes, I tried to write a code which simulates some calculations. I used BackGroundWorker to avoid GUI freeze during calculation. My problem is that I want to make change in UI, for example, changing a button's text. I added it to DoWork section of my code. Consider that my operations order needs to be kept. There is runtime error says cross-thread exception. I think I have to use Invoke and Delegate for solving my problem. Please write me a code for solving this problem. I would learn from your code.
Thanks.
  1. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)  
  2.         {  
  3.             button1.Text = "processing";  
  4.             int sum = 0;  
  5.             for (int i = 0; i <= 100; i++)  
  6.             {  
  7.                 Thread.Sleep(100);  
  8.                 sum = sum + i;  
  9.                 backgroundWorker1.ReportProgress(i);  
  10.                 if (backgroundWorker1.CancellationPending)  
  11.                 {  
  12.                     e.Cancel = true;  
  13.                     backgroundWorker1.ReportProgress(0);  
  14.                     return;  
  15.                 }  
  16.             }  
  17.             e.Result = sum;  
  18.         }  
  19.   
  20.   
  21.         private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)  
  22.         {  
  23.             progressBar1.Value = e.ProgressPercentage;  
  24.             label1.Text = e.ProgressPercentage + "%";  
  25.   
  26.         }  
  27.   
  28.         private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)  
  29.         {  
  30.             if (e.Cancelled)  
  31.             {  
  32.                 label1.Text = "Process canceled";  
  33.             }  
  34.             else if (e.Error != null)  
  35.             {  
  36.                 label1.Text = e.Error.Message;  
  37.             }  
  38.             else  
  39.             {  
  40.                 label1.Text = "Result: " + e.Result.ToString();  
  41.             }  
  42.   
  43.         }  
  44.   
  45.         private void button1_Click(object sender, EventArgs e)  
  46.         {  
  47.             if (!backgroundWorker1.IsBusy)  
  48.             {  
  49.                 backgroundWorker1.RunWorkerAsync();  
  50.             }  
  51.             else  
  52.             {  
  53.                 MessageBox.Show("Busy processing!. Please wait...");  
  54.             }  
  55.         }  
  56.   
  57.         private void button2_Click(object sender, EventArgs e)  
  58.         {  
  59.             if (backgroundWorker1.IsBusy)  
  60.             {  
  61.                 backgroundWorker1.CancelAsync();  
  62.             }  
  63.             else  
  64.             {  
  65.                 label1.Text = "No operation is running to cancel.";  
  66.             }  
  67.         }  
  68.     }  
 

Answers (2)