title: How do you use AsParallel with the async and await keywords?
body: <p>I was looking at someone sample code for async and noticed a few issues with the way it was implemented. Whilst looking at the code I wondered if it would be more efficient to loop through a list using as parallel, rather than just looping through the list normally.</p>
  
  <p>As far as I can tell there is very little difference in performance, both use up every processor, and both talk around the same amount of time to completed. </p>
  
  <p>This is the first way of doing it</p>
  
  <pre><code>var tasks= Client.GetClients().Select(async p =&gt; await p.Initialize());
  </code></pre>
  
  <p>And this is the second</p>
  
  <pre><code>var tasks = Client.GetClients().AsParallel().Select(async p =&gt; await p.Initialize());
  </code></pre>
  
  <p>Am I correct in assuming there is no difference between the two?</p>
  
  <p>The full program can be found below</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace ConsoleApplication2
  {
      class Program
      {
          static void Main(string[] args)
          {
              RunCode1();
              Console.WriteLine("Here");
              Console.ReadLine();
  
              RunCode2();
              Console.WriteLine("Here");
  
              Console.ReadLine();
  
          }
  
          private async static void RunCode1()
          {
              Stopwatch myStopWatch = new Stopwatch();
              myStopWatch.Start();
  
              var tasks= Client.GetClients().Select(async p =&gt; await p.Initialize());
  
              Task.WaitAll(tasks.ToArray());
              Console.WriteLine("Time ellapsed(ms): " + myStopWatch.ElapsedMilliseconds);
              myStopWatch.Stop();
          }
          private async static void RunCode2()
          {
              Stopwatch myStopWatch = new Stopwatch();
              myStopWatch.Start();
              var tasks = Client.GetClients().AsParallel().Select(async p =&gt; await p.Initialize());
              Task.WaitAll(tasks.ToArray());
              Console.WriteLine("Time ellapsed(ms): " + myStopWatch.ElapsedMilliseconds);
              myStopWatch.Stop();
          }
      }
      class Client
      {
          public static IEnumerable&lt;Client&gt; GetClients()
          {
              for (int i = 0; i &lt; 100; i++)
              {
                  yield return new Client() { Id = Guid.NewGuid() };
              }
          }
  
          public Guid Id { get; set; }
  
          //This method has to be called before you use a client
          //For the sample, I don't put it on the constructor
          public async Task Initialize()
          {
              await Task.Factory.StartNew(() =&gt;
                                        {
                                            Stopwatch timer = new Stopwatch();
                                            timer.Start();
                                            while(timer.ElapsedMilliseconds&lt;1000)
                                            {}
                                            timer.Stop();
  
                                        });
              Console.WriteLine("Completed: " + Id);
          }
      }
  }
  </code></pre>