Note that there are some explanatory texts on larger screens.

plurals
  1. POProper way to pass GUI values to backgroundworker?
    text
    copied!<p>I am working with a fairly complex GUI and am trying to pass a lot of data from the GUI to a backgroudWorker. The problem I am running into is accessing some of the GUI values from the background worker. For example, if I try to get <code>ComboBox.Text</code> I get a <code>InvalidOperationException</code> due to cross-threading. However, if I say do <code>TextBox.Text</code>, everything seems to work fine. Granted I am fairly new to C#, so I'm a little unclear on why some of these are OK and others fail. </p> <p>I have come up with several ways to fix my issues but am seeking the best practice from someone who is experienced in c#.</p> <p>Here are a couple ways i can think of fixing this</p> <ol> <li><p>create class/struct of all the values you want to pass to the background worker and pass this when you call RunworkAsync. I did not find this very attractive as i was having to build a class/struct for every page on my GUI to pass to the backgroundWorker</p></li> <li><p>Create a bunch of different background workers that had specific task. I still had some issues with passing data but the amount of data I had to pass was cut down quite a bit. However, the number of DoWork/ProgressChanged/RunworkerCompleted went up significantly which was less than ideal.</p></li> <li><p>(this lead me to what I'm currently doing)</p></li> </ol> <p>create a delegate and method to capture the information</p> <pre><code>private delegate string ReadComboDelegate(ComboBox c); private string ReadComboBox(ComboBox c) { if(c.InvokeRequired) { ReadComboDelegate del = new ReadComboDelegate(this.ReadComboBox); return (string) c.Invoke(del,c); } else { return c.Text } } </code></pre> <p>then within <code>DoWork</code>, do somthing like <code>string txt = this.ReadComboBox(this.comboBox1);</code></p> <p>When you have a simple GUI and you don't have to pass a lot of data this is pretty simple problem. However, the more items and complex the GUI gets the bigger this problem becomes. If anyone has any info that would make this easier, I would appreciate it.</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload