Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Questions like this seem to behave like buses. You don't see any for ages then two come along almost at the same time. See this <a href="https://stackoverflow.com/questions/5537918/silverlight-httpwebrequest-syncronous-call/5538448#5538448">answer</a> to a more concrete version of this question asked earlier today.</p> <p>I have to I agree with you, continuation passing is tricky. A really useful technique is to borrow the C# <code>yield return</code> construct to create a machine that is able to maintain state between asynchronous operations. For a really good explanation see this <a href="http://csharperimage.jeremylikness.com/2010/03/sequential-asynchronous-workflows-in.html" rel="nofollow noreferrer">blog</a> by Jeremy Likness.</p> <p>Personally I prefer a "less is more" approach so the <a href="http://geekswithblogs.net/codingbloke/archive/2010/09/12/simple-asynchronous-operation-runner-ndash-part-1.aspx" rel="nofollow noreferrer">AsyncOperationService</a> is a very small chunk of code. You'll note that it has a single callback for both success and failure and there no interfaces to implement just a moderate delegate <code>Action&lt;Action&lt;Exception&gt;&gt;</code> which is typed as <code>AsyncOperation</code> to make it more convenient.</p> <p>The basic steps to coding against this are:-</p> <ul> <li>Code as if synchronous execution were possible</li> <li>Create methods that return an <code>AsyncOperation</code> fpr only the smallest part that has to be asynchronous. Usually some <code>WebRequest</code> or WCF call but note just enough to get past the async bit, see me other answer for a good example.</li> <li>Convert the synchronous "psuedo-code" to <code>yeild</code> these AsyncOperations and change the calling code to "Run" the resulting enumerable.</li> </ul> <p>The final code looks quite similar to the synchronous code you might be more familar with.</p> <p>As to accidentally running things on a background thread, that last answer included this useful AsyncOperation:-</p> <pre><code>public static AsyncOperation SwitchToUIThread() { return (completed =&gt; Deployment.Current.Dispatcher.BeginInvoke(() =&gt; completed(null))); } </code></pre> <p>You can use that as the final <code>yield</code> in the run to ensure that code executing in the <code>completed</code> callback is executing on the UI thread. Its also useful to "flip" what is apparently synchronous code to be running on the UI thread when necessary.</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