Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, there are two separate issues here:</p> <ul> <li>Updating the UI from a different class</li> <li>Updating the UI from a different thread</li> </ul> <p>The two are almost completely orthogonal. Once you've solved each of them in isolation, you can put the two together.</p> <p>If there are well-understood ways in which the UI needs to be changed (e.g. updating the text in a status bar or something like that) I would write methods in the UI class itself to do that, and just call those methods from the other class. Alternatively, you <em>can</em> expose the individual control of the UI - preferably through properties - and then use <code>Control.Invoke</code> in the normal way from the other class, e.g.</p> <pre><code>MethodInvoker action = () =&gt; form.StatusLabel.Text = "Finished"; form.BeginInvoke(action); </code></pre> <p>So long as you call <code>Invoke</code> on a control which "lives" on the same thread as all the controls you touch, it doesn't matter which one you use... so an alternative could be:</p> <pre><code>Label label = form.StatusLabel; MethodInvoker action = () =&gt; label.Text = "Finished"; label.BeginInvoke(action); </code></pre> <p>One difference here is that in this latter code, the <code>StatusLabel</code> property is being evaluated in the worker thread - which <em>could</em> lead to race conditions if your UI is changing it to refer to different labels at different times. I would generally prefer the first approach.</p> <p>(I agree with icktoofay's comment about <code>BeginInvoke</code> being preferable unless you really need to wait, by the way... although you do then need to be careful about changes to any variables captured by anonymous functions.)</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