title: Recursive Task Queue
body: <p>I'm trying to iterate through a queue - taking 1 item from the queue, processing it in a background task, updating the UI, and then taking the next item, and so on.  The problem is the first item is processed in a background task (thread) but then the subsequent items are processed in the UI thread - blocking the UI.</p>
  
  <p>Does anyone know why this happens and how to get around this problem?  My full test code is below.  Note: this code is for my learning and future reference - not any real application.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public partial class MainWindow : Window
  {
      private Queue&lt;int&gt; testQueue = new Queue&lt;int&gt;();
      private TaskScheduler uiScheduler;
  
      public MainWindow()
      {
          InitializeComponent();
  
          this.uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
          this.testQueue = new Queue&lt;int&gt;();
          this.testQueue.Enqueue(3);
          this.testQueue.Enqueue(6);
          this.testQueue.Enqueue(7);
          this.testQueue.Enqueue(11);
          this.testQueue.Enqueue(13);
      }
  
      // just a method that takes about 1 second to run on a modern pc
      private double SumRootN(int root)
      {
          double result = 0;
          for (int i = 1; i &lt; 10000000; i++)
          {
              result += Math.Exp(Math.Log(i) / root);
          }
          return result;
      }
  
      private void testQueueButton_Click(object sender, RoutedEventArgs e)
      {
          this.processQueue();
      }
  
      private void processQueue()
      {
          if (this.testQueue.Count &gt; 0)
          {
              int root = this.testQueue.Dequeue();
              Task&lt;double&gt;.Factory.StartNew(() =&gt; SumRootN(root))
                  .ContinueWith(t =&gt;
                  {
                      this.statusText.Text += String.Format("root {0} : {1}\n", root, t.Result);
                      this.processQueue();
                  }, uiScheduler);
          }
          else
          {
              this.statusText.Text += "Done\n";
          }
      }
  }
  </code></pre>