Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no universal 'best' way to thread work. You just have to try different ways of doing things, I'm afraid. </p> <p>I particularly like Jeremy D. Miller's continuation idea described <a href="http://msdn.microsoft.com/en-us/magazine/ee309512.aspx" rel="nofollow noreferrer">at this page</a> (scroll down to find the "continuations" section). It's really elegant and means writing very little boilerplate code.</p> <p>Basically, when you call "ExecuteWithContinuation" with a Func argument, the function is executed asynchronously, then returns an action when it finishes. The action is then marshalled back onto your UI thread to act as a continuation. This allows you to quickly split your operations into two bits: </p> <ol> <li>Perform long running operation that shouldn't block the UI</li> <li>... when finished, update the UI on the UI thread </li> </ol> <p>It takes a bit of getting used to, but it's pretty cool. </p> <pre><code>public class AsyncCommandExecutor : ICommandExecutor { private readonly SynchronizationContext m_context; public AsyncCommandExecutor(SynchronizationContext context) { if (context == null) throw new ArgumentNullException("context"); m_context = context; } public void Execute(Action command) { ThreadPool.QueueUserWorkItem(o =&gt; command()); } public void ExecuteWithContinuation(Func&lt;Action&gt; command) { ThreadPool.QueueUserWorkItem(o =&gt; { var continuation = command(); m_context.Send(x =&gt; continuation(), null); }); } } </code></pre> <p>You'd then use it like this (forgive the formatting...)</p> <pre><code>public void DoSomethingThatTakesAgesAndNeedsToUpdateUiWhenFinished() { DisableUi(); m_commandExecutor.ExecuteWithContinuation( () =&gt; { // this is the long-running bit ConnectToServer(); // This is the continuation that will be run // on the UI thread return () =&gt; { EnableUi(); }; }); } </code></pre>
    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. 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.
 

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