title: Top Level Task Causes Error "The current SynchronizationContext may not be used as a TaskScheduler."
body: <p>I do not see the answer yet, although I have looked on stackoverflow and documentation.</p>
  
  <p>This error is coming up over the ContinueWidth below if I call the code that creates the tasks from a timer handler or within another task. For example, if it is wrapped in another task that creates tasks every interval (e.g. every 1 second) as follows. The only reason why I have the high level task is to create these lower tasks every interval.</p>
  
  <p>//Higher level task creation</p>
  
  <p>...</p>
  
  <pre><code>Task task = Task.Factory.StartNew(new Action(UpdateAllDuringInterval));
  </code></pre>
  
  <p>...</p>
  
  <pre><code>    private void UpdateAllDuringInterval()
      {
          Stopwatch stopWatch = new Stopwatch();
  
          do
          {
              // Start the stopwatch
              stopWatch.Start();
  
              // Create tasks
              List&lt;Task&gt; tasks = CreateTasksAndStart();
  
              // Wait for the tasks to complete if testing, since want to check results
              if (this._testMode)
              {
                  Task[] taskArray = tasks.ToArray();
                  Task.WaitAll(taskArray);
              }
  
              if (!_testMode)
              {
                  // Get remaining time to complete interval and sleep for that time
                  int remainingTimeInMilliseconds = this._pollingIntervalInMilliseconds -
                                                    (int) stopWatch.Elapsed.TotalMilliseconds;
                      // truncating milliseconds
                  if (remainingTimeInMilliseconds &gt; 0)
                      Thread.Sleep(remainingTimeInMilliseconds);
                          // will give time to CPU. Note that using Sleep used to be frowned upon but no longer due to advantages in multitasksing/CPU utilization
              }
          } while (!_testMode); // continue updating stocks once per interval if not testing
      }
  
      private List&lt;Task&gt; CreateTasksAndStart()
      {
          var tasks = new List&lt;Task&gt;();
  
          lock (syncLock)
          {
              for (int i = 0; i &lt; _stocksToUpdate.Count; i++)
              {
                  var item = _stocksToUpdate[i];
                  var initialTask = Task.Factory.StartNew(() =&gt;
                  {
                      GetUpdatedStockInformation(item);
                  });
                  var continuationTask = initialTask.ContinueWith((antecendent) =&gt;
                  {
                      UpdateUIWithUpdatedStockInformation();
                  }
                  , CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion,
                  TaskScheduler.FromCurrentSynchronizationContext());
  
                  // For testing, we want to wait on the continuation task to make sure updates are done
                  tasks.Add(continuationTask);
              }
          }
          return tasks;
      }
  </code></pre>
  
  <p>If more information is needed, just let me know. Feel free to give me other pitfalls of this code as well. Thanks!</p>