title: Multi threading using async/await and httpclient in c#
body: <p>I wrote a console application for downloading YouTube preview-images.But I think this program is running synchronously instead async. What did I do wrong and how do I make multi-loading files from web use async/await?</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Net.Http;
  using System.Text.RegularExpressions;
  using System.Threading.Tasks;
  
  
  namespace YoutubePreviewer
  {
      class Node
      {
          public string Path { get; private set; }
          public string Title { get; private set; }
          public string Source { get; private set; }
          public string Id { get; private set; }
          public Previews Previews { get; set; }
  
          public Node(string p, string t, string s, string i)
          {
              Path = p;
              Title = t;
              Source = s;
              Id = i;
          }
  
      }
  
      class Previews
      {
          public string[] Urls { get; private set; }
  
  
          public static Previews Get(Node n)
          {
              string[] resolutions = {"default", "hqdefault", "mqdefault", "maxresdefault"};
              for (int i = 0; i &lt; resolutions.Length; i++)
              {
                  string end = resolutions[i] + ".jpg";
                  resolutions[i] = "https://img.youtube.com/vi/" + n.Id + "/" + resolutions[i] + ".jpg";
              }
              Previews pr = new Previews();
              pr.Urls = resolutions;
              return pr;
          }
      }
  
      static class Operations
      {
          public static async Task&lt;string&gt; DownloadUrl(string address)
          {
              HttpClient http = new HttpClient();
              return await http.GetStringAsync(address);
          }
  
          public static async Task&lt;Node&gt; Build(string url)
          {
              var source = await Operations.DownloadUrl(url);
              var title = Regex.Match(source, "&lt;title&gt;(.*)&lt;/title&gt;").Groups[1].Value;
              var id = Regex.Match(url, @"watch\?v=(.+)").Groups[1].Value;
              Node node = new Node(url, title, source, id);
              node.Previews =await Task&lt;Previews&gt;.Factory.StartNew(()=&gt;Previews.Get(node);
              return node;
          }
  
          public static async Task WriteToDisk(Node n, string path = "C:/Downloads")
          {
              Console.WriteLine($"Starting downloading {n.Path} previews");
              var securedName = string.Join("_", n.Title.Split(Path.GetInvalidFileNameChars()));
  
              Directory.CreateDirectory(Path.Combine(path, securedName));
              HttpClient http = new HttpClient();
              foreach (var preview in n.Previews.Urls)
              {
                  try
                  {
                      var arr = await http.GetByteArrayAsync(preview);
                      await Task.Delay(100);
                      string name = preview.Substring(preview.LastIndexOf("/") + 1);
                      using (FileStream fs = new FileStream(Path.Combine(path, securedName, name), FileMode.Create,
                          FileAccess.ReadWrite))
                      {
                          await fs.WriteAsync(arr, 0, arr.Length);
                      }
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine($"Can't download and save preview  {preview}");
                      Console.WriteLine(e.Message);
                      Console.WriteLine(new string('*', 12));
                  }
                  Console.WriteLine($"{preview} is saved!");
              }
  
          }
  
          public static async Task Load(params string[] urls)
          {
  
              foreach (var url in urls)
              {
                  Node n = await Build(url);
                  await WriteToDisk(n);
  
              }
          }
      }
  
      class Program
      {
          static  void Main(string[] args)
          {
  
              Task t= Operations.Load(File.ReadAllLines("data.txt"));
  
              Task.WaitAll(t);
  
              Console.WriteLine("Done");
              Console.ReadKey();
  
  
          }
  
  
      }
  }
  </code></pre>