Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to think about the problem differently. To make asynchronous things "feel" synchronous, the easiest way to do it is to restructure your code to make use of '<a href="http://en.wikipedia.org/wiki/Continuation-passing_style" rel="nofollow noreferrer">continuation passing style</a>'.</p> <p>In essence, instead of calling a function that returns a value and then you process that value, you call a function, passing an anonymous function as a delegate to it. The called function will then invoke the delegate, passing in the string.</p> <p>Here is an example, which uses anonymous functions and lambdas:</p> <pre><code>void DoSomethingAsync( Action&lt;string&gt; callback ) { HttpWebRequest req; // TODO: build your request req.BeginGetResponse( result =&gt; { // This anonymous function is a closure and has access // to the containing (or enclosing) function. var response = req.EndGetResponse( result ); // Get the result string and call the callback string resultString = null; // TODO: read from the stream callback(resultString); }, null ); } </code></pre> <p>This is one half the solution. The next part, is to actually call this. Imagine you have an ICommand instance or simpler, a button click event that needed to call this function and "get the string". Instead of "getting the string" you call this function and supply a callback method (which will be a closure).</p> <pre><code>void btnGo_Click( object sender, EventArgs e ) { DoSomethingAsync( resultString =&gt; { // This anonymous function is called when the web request has // finished and has your string. // Now that we have the string, we can go and process it. ProcessWebResponseResult( resultString ); }); } </code></pre> <p>Here is a really good article explaining the concept further: <a href="http://blogs.msdn.com/b/wesdyer/archive/2007/12/22/continuation-passing-style.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/b/wesdyer/archive/2007/12/22/continuation-passing-style.aspx</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.
 

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