title: TPL - running a series of .net 4.5 rest queries in order
body: <p>I'm a bit of a TPL newbie. Not sure whether this library is the way to go, but I would like to run a set of asynchronous tasks in order, but not block the UI, but run in order and callback in the order that they were started - a bit like running them synchronously. so effectively only callback when the task is finished and has a HttpResponseMessage.. and only then start the next task.</p>
  
  <p>So the behaviour would be </p>
  
  <p>Run long running task 1, when its complete callback so that we can do something with the UI, then
  Run the next long running task, when its complete callback so that we can update the UI.
  Run a shorter task, when its complete callback so that we can update the UI</p>
  
  <p>I have seen some examples where a scheduler is used but anyway here is my code</p>
  
  <pre><code>private void RunSequentially()
  {
          var lcts = new LimitedConcurrencyLevelTaskScheduler(1);
          var factory = new TaskFactory(lcts);
          string username = "user";
          string password = "password";
          var handler = new HttpClientHandler
              {
                 Credentials = new NetworkCredential(username, password), PreAuthenticate = true 
              };
          var client = new HttpClient(handler);
  
          List&lt;Tuple&lt;string,string,string&gt;&gt; taskTuples = new List&lt;Tuple&lt;string, string, string&gt;&gt;();
          taskTuples.Add(new Tuple&lt;string, string, string&gt;("POST", "http://longrunning", XDocument.Load(@"./somefile.xml").ToString()));
          taskTuples.Add(new Tuple&lt;string, string, string&gt;("GET", "http://getItem", null));
  
          factory.StartNew(
              () =&gt;
                  {
                      foreach(var tuple in taskTuples)
                      {
                          ProcessSequentially(tuple,ProcessCallback, client);
                      }
                  });
      }
  </code></pre>
  
  <p>And the ProcessSequentially method</p>
  
  <pre><code>private async void ProcessSequentially(Tuple&lt;string, string, string&gt; tuple, Action&lt;string&gt; callback, HttpClient client)
      {
          HttpResponseMessage message = null;
          if (tuple.Item1 == "POST")
          {
              message = await
                  client.PostAsync(
                      tuple.Item2,
                      new StringContent(tuple.Item3));
          }
          else
          {
              message = await
                  client.GetAsync(
                      tuple.Item2);
          }
  
          callback(message.ReasonPhrase);
  
  
      }
  </code></pre>
  
  <p>Now assuming that the post task will be the long running one - I need the long running one to start first and callback first and then for the short running GET to start after the first one has called back without having to block the UI via a WaitAll etc. Is there any way to do this perhaps as a transaction in TPL or TPL dataflow because at the moment when I run this the POST does get started first but because the get is a fast operation it completes first and returns first and I would like them to happen in sequence as in a synchronous operation...</p>
  
  <p>So in order to change the code - would this be a better way to go?</p>
  
  <pre><code>foreach (var tuple in taskTuples)
          {
              factory.StartNew(
              () =&gt;
              {
                  this.ProcessSequentially(tuple,client);
              }).ContinueWith(this.FinishSequentialTask);
          }
  </code></pre>