Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well... Hope this helps...</p> <p>To run a method in other Thread, simply do:</p> <pre><code>new System.Threading.Thread(() =&gt; YourMethodName()).Start(); </code></pre> <p>This can be used multiple times.</p> <p>Note that this method returns void and does not receive any parameters.</p> <p>To achieve what you want, you need to do:</p> <pre><code>for (int i = 0; i &lt;= 500; i++) { new System.Threading.Thread(() =&gt; YourMethodName()).Start(); } </code></pre> <p>Notes:</p> <p>a) With this code, you dont know when a thread will end. To verify when a thread is finished, you need to use the .IsAlive property. Example:</p> <pre><code>Thread t = new System.Threading.Thread(() =&gt; YourMethodName()); t.Start(); </code></pre> <p>To verify:</p> <pre><code>if (t.IsAlive) { // running } else { // finished } </code></pre> <p>2) Exceptions cannot be handled from outside. You need to handle all the exceptions inside the thread, otherwise the program will break if an exception is raised.</p> <p>3) You can't access UI elements inside the thread. To access the UI elements, you will need to use the Dispatcher.</p> <p><strong>Edit</strong></p> <p>You can do more things in other thread than just firing a method. </p> <p>You can pass parameters:</p> <pre><code>new System.Threading.Thread(() =&gt; YourMethodName(a, b, c)).Start(); </code></pre> <p>You can run more than a single method:</p> <pre><code>new System.Threading.Thread(() =&gt; { YourMethodName(a, b, c); OtherMethod(a); }).Start(); </code></pre> <p>And you can receive values returned from the thread:</p> <pre><code>int x = 0; new System.Threading.Thread(() =&gt; { x = YourMethodName(); }).Start(); </code></pre> <p>To know when the x receive the value from the thread, you can do (lets suppose an int): </p> <pre><code>int x { set { VariableSetted(value); } // fire the method } // put it in global scope, outside the method body </code></pre> <p>and</p> <pre><code>new System.Threading.Thread(() =&gt; { x = YourMethodName(); }).Start(); </code></pre> <p>and a method to run when the thread has returned the value:</p> <pre><code>public void VariableSetted(int x) { // Do what you want with the value returned from thread // Note that the thread started this method, so if you need to // update UI objects, you need to use the dispatcher. } </code></pre> <p>I don't know if you are using WPF to make the UI, but if is, to update the screen you can do this:</p> <pre><code>new System.Threading.Thread(() =&gt; { string result = YourMethodName(); this.Dispatcher.Invoke((Action)(() =&gt; { yourTextBox.Text = result; })); }).Start(); </code></pre> <p>You can also start nested threads (thread inside thread).</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