Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>All controls have a method called Invoke, which takes a delegate as the first argument and optional <em>params object[]</em>.<br> You can use this method easily:</p> <pre><code>richTextBox1.Invoke(new MethodInvoker(DoSomething)); </code></pre> <p>where </p> <pre><code>void DoSomething() { richTextBox1.BackColor = Color.Cyan; } </code></pre> <p>The delegate MethodInvoker is in the System.Windows.Forms namespace, which, I suppose, you are already using. </p> <p>You can even invoke from the same thread! </p> <p>You can also use parameters, like this: </p> <pre><code>richTextBox1.Invoke(new ColorChanger(DoSomething), Color.Cyan); </code></pre> <p>where </p> <pre><code>delegate void ColorChanger(Color c); void DoSomething(Color c) { richTextBox1.BackColor = c; } </code></pre> <p>I hope this helped! </p> <p>Edit:<br> <code>InvokeRequired</code> is required if you are using the same method from a... basically... unknown thread. So it would look like this: </p> <pre><code>void DoSomething() { if (richTextBox1.InvokeRequired) richTextBox1.Invoke(new MethodInvoker(DoSomething)); else { richTextBox1.BackColor = Color.Cyan; // Here should go everything the method will do. } } </code></pre> <p>You may call this method from ANY thread! </p> <p>And for parameters:</p> <pre><code>delegate void ColorChanger(Color c); void DoSomething(Color c) { if (richTextBox1.InvokeRequired) richTextBox1.Invoke(new ColorChanger(DoSomething), c); else { richTextBox1.BackColor = c; // Here should go everything the method will do. } } </code></pre> <p>Enjoy programming!</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