title: C# running async tasks in sync context
body: <p>I don't have much experience in C# async. </p>
  
  <p>Task - load bitmaps from the internet. Before, I was just loading them 1 by 1, in sync. Loading them in async would give results quicker. Down below, I made two examples, how I could get single image - <code>GetImage</code> and <code>GetImageAsync</code>. And for a list of Images, I would use <code>LoadImages</code> and <code>LoadImages2</code>.</p>
  
  <p><code>LoadImages</code> would run sync functions in async (all at the same time (?)), <code>LoadImages2</code> would run async functions in async and produce the same result (?).
  The thing, that I don't fully understand - in <code>GetImageAsync</code> <code>await request.GetResponseAsync()</code>. Do I really need it? Is it a "better" way of doing the same thing? Are there really any difference between <code>LoadImages</code> and <code>LoadImages2</code>. </p>
  
  <p>At the moment, I am thinking of choosing <code>GetImage</code> and <code>LoadImages</code> option. Also, I don't want to decorate every function with <code>async Task</code>, I only need to load these images in async.</p>
  
  <pre><code>public Bitmap GetImage(string url)
  {
      HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
      using (WebResponse response = request.GetResponse())
      using (Stream responseStream = response.GetResponseStream())
          return new Bitmap(responseStream);
  }
  
  public async Task&lt;Bitmap&gt; GetImageAsync(string url)
  {
      HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
      using (WebResponse response = await request.GetResponseAsync())
      using (Stream responseStream = response.GetResponseStream())
          return new Bitmap(responseStream);
  }
  
  private Dictionary&lt;string, Bitmap&gt; LoadImages(List&lt;string&gt; urls)
  {
      Dictionary&lt;string, Bitmap&gt; images = new Dictionary&lt;string, Bitmap&gt;();
      Task.WaitAll(urls.Select(url =&gt; 
          Task.Run(() =&gt;
          {
              images.Add(url, GetImage(url));
          })).ToArray());
      return images;
  }
  
  private Dictionary&lt;string, Bitmap&gt; LoadImages2(List&lt;string&gt; urls)
  {
      Dictionary&lt;string, Bitmap&gt; images = new Dictionary&lt;string, Bitmap&gt;();
      Task.WhenAll(urls.Select(url =&gt;
          Task.Run(async () =&gt;
          {
              images.Add(url, await GetImageAsync(url));
          })));
      return images;
  }
  </code></pre>