title: Calling an async method from a synchronous method
body: <p>I am attempting to run async methods from a synchronous method.  But I can't <em>await</em> the <em>async</em> method since I am in a synchronous method.  I must not be understanding TPL as this is the fist time I'm using it.  </p>
  
  <pre><code>private void GetAllData()
  {
      GetData1()
      GetData2()
      GetData3()
  }
  </code></pre>
  
  <p>Each method needs the previous method to finish as the data from the first is used for the second.</p>
  
  <p>However, inside each method I want to start multiple <code>Task</code> operations in order to speed up the performance.  Then I want to wait for all of them to finish.  </p>
  
  <p>GetData1 looks like this</p>
  
  <pre><code>    internal static void GetData1 ()
      {
          const int CONCURRENCY_LEVEL = 15; 
          List&lt;Task&lt;Data&gt;&gt; dataTasks = new List&lt;Task&lt;Data&gt;&gt;();
          for (int item = 0; item &lt; TotalItems; item++)
          {
              dataTasks.Add(MyAyncMethod(State[item]));
          }
          int taskIndex = 0;
          //Schedule tasks to concurency level (or all)
          List&lt;Task&lt;Data&gt;&gt; runningTasks = new List&lt;Task&lt;Data&gt;&gt;();
          while (taskIndex &lt; CONCURRENCY_LEVEL &amp;&amp; taskIndex &lt; dataTasks.Count)
          {
              runningTasks.Add(dataTasks[taskIndex]);
              taskIndex++;
          }
  
          //Start tasks and wait for them to finish
          while (runningTasks.Count &gt; 0)
          {
              Task&lt;Data&gt; dataTask = await Task.WhenAny(runningTasks);
              runningTasks.Remove(dataTask);
              myData = await dataTask;
  
  
              //Schedule next concurrent task
              if (taskIndex &lt; dataTasks.Count)
              {
                  runningTasks.Add(dataTasks[taskIndex]);
                  taskIndex++;
              }
          }
          Task.WaitAll(dataTasks.ToArray()); //This probably isn't necessary
      }
  </code></pre>
  
  <p>I am using await here but get an Error  </p>
  
  <blockquote>
    <p>The 'await' operator can only be used within an async method. Consider
    marking this method with the 'async' modifier and changing its return
    type to 'Task'</p>
  </blockquote>
  
  <p>However, if I use the async modifier this will be an asynchronous operation. Therefore, if my call to <code>GetData1</code> doesn't use the await operator won't control go to GetData2 on the first await, which is what I am trying to avoid?  Is it possible to keep GetData1 as a synchronous method that calls an asynchronous method?  Am I designing the Asynchronous method incorrectly? As you can see I'm quite confused.</p>
  
  <p>This could be a duplicate of <a href="https://stackoverflow.com/q/9343594/2145211">How to call asynchronous method from synchronous method in C#?</a>  However, I'm not sure how to apply the solutions provided there as I'm starting multiple tasks, want to <code>WaitAny</code>, do a little more processing for that task, then wait for all tasks to finish before handing control back to the caller.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here is the solution I went with based on the answers below:</p>
  
  <pre><code>    private static List&lt;T&gt; RetrievePageTaskScheduler&lt;T&gt;(
          List&lt;T&gt; items,
          List&lt;WebPageState&gt; state,
          Func&lt;WebPageState, Task&lt;List&lt;T&gt;&gt;&gt; func)
      {
          int taskIndex = 0;
  
          // Schedule tasks to concurency level (or all)
          List&lt;Task&lt;List&lt;T&gt;&gt;&gt; runningTasks = new List&lt;Task&lt;List&lt;T&gt;&gt;&gt;();
          while (taskIndex &lt; CONCURRENCY_LEVEL_PER_PROCESSOR * Environment.ProcessorCount
              &amp;&amp; taskIndex &lt; state.Count)
          {
              runningTasks.Add(func(state[taskIndex]));
              taskIndex++;
          }
  
          // Start tasks and wait for them to finish
          while (runningTasks.Count &gt; 0)
          {
              Task&lt;List&lt;T&gt;&gt; task = Task.WhenAny(runningTasks).Result;
              runningTasks.Remove(task);
  
              try
              {
                  items.AddRange(task.Result);
              }
              catch (AggregateException ex)
              {
                  /* Throwing this exception means that if one task fails 
                   * don't process any more of them */
  
                  // https://stackoverflow.com/questions/8853693/pattern-for-implementing-sync-methods-in-terms-of-non-parallel-task-translating
                  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(
                      ex.Flatten().InnerExceptions.First()).Throw();
              }
  
              // Schedule next concurrent task
              if (taskIndex &lt; state.Count)
              {
                  runningTasks.Add(func(state[taskIndex]));
                  taskIndex++;
              }
          }
  
          return items;
      }
  </code></pre>