Question title: "Updating the GUI from the desktop", but the correct world of names: "Updating the GUI from the desktop or reporting several variables (other than the whole) from the desktop"
Please let me explain my situation. In the program, I have a background worker who analyzes information. As a result of this analysis, the elements of the GUI form should be filled with the necessary data. In the GUI, I would like to update
- 2 datagridviews
- 1 listbox
- 5 shortcuts
As I understand it - I can only state 1 int value through the ReportProgress() background worker method.
So, the question is how to pass List<> (+ some other variables: string , int ) via ReportProgress() ? Basically - I want to update the GUI with the help of information, but "1 integer" just will not. Therefore, you can either pass several variables through ReportProgress() OR I can use Invoke from inside BackgroundWorker to update the GUI myself .. Personally, I donβt like the Invoke approach ... What do you think?
Here is my code (see comments):
private void button9_Click(object sender, EventArgs e) // start BW { bw.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); bw.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); bw.WorkerReportsProgress = true; bw.WorkerSupportsCancellation = true; bw.RunWorkerAsync(10); } private void button10_Click(object sender, EventArgs e) // cancel BW { bw.CancelAsync(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { int count = (int)e.Argument; for (int i = 1; i <= count; i++) { if (bw.CancellationPending) { e.Cancel = true; break; } List<List<string>> list_result = new List<List<string>>(); list_result = Proccess(); bw.ReportProgress(list_result.Count()); // right now I can only return a single INT /////////// UPDATE GUI ////////////// // change datagridview 1 based on "list_result" values // change datagridview 2 // change listbox // change label 1 // change label .. Thread.Sleep(20000); } MessageBox.Show("Complete!"); e.Result = sum; } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { prog_count++; listBox1.Items.Add("Count: (" + prog_count.ToString() + "/20). Found: " + e.ProgressPercentage.ToString() + "."); }
source share