Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Any time you touch a control you must do so from the UI thread. You can do the actual downloading of the picture and other long-running tasks from your thread/task. From your thread you use Control.Invoke to invoke a delegate on the UI thread.</p> <p>This MSDN article has a good example of using Control.Invoke with simple variables like strings and such:</p> <p><a href="http://msdn.microsoft.com/en-us/library/a1hetckb(v=vs.100).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/a1hetckb(v=vs.100).aspx</a></p> <p>But often a helper class is used so you can pass more complex data between your delegates without resorting to big nasty object arrays.</p> <p>Here's some sample code I did up:</p> <pre><code> private void button1_Click(object sender, EventArgs e) { Task.Factory.StartNew(() =&gt; { DataClass dataClass = new DataClass(this.textBox1); Thread.Sleep(5000); // simulate long running task dataClass.TextToPass = "set some text"; dataClass.updateTargetControl(); }); } private class DataClass { delegate void updateTextBoxOnUiThreadDelegate(); private TextBox _targetControl= null; private updateTextBoxOnUiThreadDelegate _updateDelegate; internal DataClass(TextBox targetControl) { _updateDelegate = new updateTextBoxOnUiThreadDelegate(updateOnUiThread); _targetControl = targetControl; } internal string TextToPass = ""; internal void updateTargetControl() { _targetControl.Invoke(_updateDelegate); } private void updateOnUiThread() { _targetControl.Text = this.TextToPass; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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