Note that there are some explanatory texts on larger screens.

plurals
  1. POContinuing a Task<T> to a Task<U> without blocking on Result
    text
    copied!<p>I have two asynchronous methods <code>Task&lt;int&gt; DoInt()</code> and <code>Task&lt;string&gt; DoString(int value)</code>. I have a third asynchronous method <code>Task&lt;string&gt; DoBoth(int value)</code> whose goal is to asynchronously execute <code>DoInt()</code>, piping it's output to <code>DoString(int)</code> and having the result be the result of <code>DoBoth()</code>.</p> <p>The important constraints are:</p> <ol> <li>I may not have source code to <code>DoInt()</code> or <code>DoString()</code>, so I can't modify them.</li> <li>I don't want to block at any point</li> <li>Output (result) of one task must be passed as input to the next</li> <li>The final output (result) is what I want to be the result of <code>DoBoth()</code></li> </ol> <p>The key being I already have methods that are asynchronous (i.e. return Task) and I want to pipe data from task to task, without blocking along the way, returning the final result in the initial task. I can do all the following except not blocking in the following code:</p> <p>Code example:</p> <pre><code>// Two asynchronous methods from another library, i.e. can't be changed Task&lt;int&gt; DoInt(); Task&lt;string&gt; DoString(int value); Task&lt;string&gt; DoBoth() { return DoInt().ContinueWith&lt;string&gt;(intTask =&gt; { // Don't want to block here on DoString().Result return DoString(intTask.Result).Result; }); } </code></pre> <p>Since <code>Task&lt;string&gt; DoString(int)</code> is already an asynchronous method, how do I cleanly create a non-blocking continuation to it? Effectively I want to create a continuation from an existing Task and not from a Func.</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