Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should look into the .NET Asynchronous Programming Model. It's much simpler then it sounds. You simply create a delegate to a method that you want to run asynchronously. Then call BeginInvoke on the delegate and your method is automatically run on a background thread.</p> <p>In the asynchronous method, if you need to update the UI, you must use Control.Invoke/BeginInvoke on the relevant form or control. </p> <p>Also, if the asynchronous method needs to return some result to the UI thread, you will need to utilise the IAsyncResult (returned from BeginInvoke) and the EndInvoke method. </p> <p>See the article <a href="http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx" rel="nofollow noreferrer">Calling Synchronous Methods Asynchronously</a> for more details.</p> <p>On the other hand, if you're just looking for an equivalent to the javascript method window.settimeout() in Windows.Forms the following avoids threads entirely by wrapping the standard Timer in a re-usable method:</p> <pre><code>public void SetTimeout(EventHandler doWork, int delayMS) { System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); timer.Interval = delayMS; timer.Tick += delegate(object s, EventArgs args) { timer.Stop(); }; timer.Tick += doWork; timer.Start(); } </code></pre> <p>then to call it:</p> <pre><code> SetTimeout(delegate { YourMethod(); }, 100); </code></pre> <p>Just like the Timer you drag onto a form, YourMethod() is run on the UI thread by the form message loop. Therefore you do not need to worry about BeginInvoke/Invoke at all.</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