Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Which version of C# are you using? If you are using C#3.5 you can use closures to avoid passing in parameters.</p> With C#3.5 <pre><code>public static class ControlExtensions { public static TResult InvokeEx&lt;TControl, TResult&gt;(this TControl control, Func&lt;TControl, TResult&gt; func) where TControl : Control { return control.InvokeRequired ? (TResult)control.Invoke(func, control) : func(control); } public static void InvokeEx&lt;TControl&gt;(this TControl control, Action&lt;TControl&gt; func) where TControl : Control { control.InvokeEx(c =&gt; { func(c); return c; }); } public static void InvokeEx&lt;TControl&gt;(this TControl control, Action action) where TControl : Control { control.InvokeEx(c =&gt; action()); } } </code></pre> <p>Safely invoking code now becomes trivial.</p> <pre><code>this.InvokeEx(f =&gt; f.label1.Text = "Hello World"); this.InvokeEx(f =&gt; this.label1.Text = GetLabelText("HELLO_WORLD", var1)); this.InvokeEx(() =&gt; this.label1.Text = DateTime.Now.ToString()); </code></pre> <hr> With C#2.0 it becomes less trivial <pre><code>public class MyForm : Form { private delegate void UpdateControlTextCallback(Control control, string text); public void UpdateControlText(Control control, string text) { if (control.InvokeRequired) { control.Invoke(new UpdateControlTextCallback(UpdateControlText), control, text); } else { control.Text = text; } } } </code></pre> <p>Using it simple, but you have to define more callbacks for more parameters.</p> <pre><code>this.UpdateControlText(label1, "Hello world"); </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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