Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to either use <code>BackgroundWorker</code>, or <code>Control</code>.<code>Invoke</code>/<code>BeginInvoke</code>. Anonymous functions - either anonymous methods (C# 2.0) or lambda expressions (C# 3.0) make this easier than it was before.</p> <p>In your case, you can change your code to:</p> <pre><code>public bool AddUser(string user, RichTextBox textBox1, List listBox1) { MethodInvoker action = delegate { textBox1.Text += "Connected to server... \n"; }; textBox1.BeginInvoke(action); } </code></pre> <p>A few things to note:</p> <ul> <li>To conform with .NET conventions, this should be called <code>AddUser</code></li> <li>You don't need to pass the textbox or listbox by reference. I suspect you don't quite understand what <code>ref</code> really means - see <a href="http://pobox.com/~skeet/csharp/parameters.html" rel="noreferrer">my article on parameter passing</a> for more details.</li> <li>The difference between <code>Invoke</code> and <code>BeginInvoke</code> is that <code>BeginInvoke</code> won't wait for the delegate to be called on the UI thread before it continues - so <code>AddUser</code> may return before the textbox has <em>actually</em> been updated. If you don't want that asynchronous behaviour, use <code>Invoke</code>.</li> <li>In many samples (including some of mine!) you'll find people using <code>Control.InvokeRequired</code> to see whether they need to call <code>Invoke</code>/<code>BeginInvoke</code>. This is actually overkill in most cases - there's no real <em>harm</em> in calling <code>Invoke</code>/<code>BeginInvoke</code> even if you don't need to, and often the handler will <em>only</em> ever be called from a non-UI thread anyway. Omitting the check makes the code simpler.</li> <li>You can also use <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" rel="noreferrer"><code>BackgroundWorker</code></a> as I mentioned before; this is particularly suited to progress bars etc, but in this case it's probably just as easy to keep your current model.</li> </ul> <p>For more information on this and other threading topics, see <a href="http://pobox.com/~skeet/csharp/threads" rel="noreferrer">my threading tutorial</a> or <a href="http://www.albahari.com/threading/" rel="noreferrer">Joe Albahari's one</a>.</p>
    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.
    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