title: How can you multithread methods within loops that take much time?
body: <p>I am working on some code which has the goal of connecting to an API to get usernames. </p>
  
  <p>Currently, I have the code working in a loop, to which arises the issue of runtime. Each "list of usernames" can be in the thousands, which can take hours to obtain them all. My goal is to figure out how to make it threaded, to allow multiple lookups at once.</p>
  
  <p>Here is my code, and the important information needed to understand it:</p>
  
  <pre><code>using Newtonsoft.Json.Linq;
  
  public List&lt;LeaderboardItem&gt; LeaderboardList;
  public LeaderboardItem LBItem;
  public GetNames getNames;
  
  public GetLeaderboardRuns(string GameCode, string Category, string Query)
  {
      LeaderboardList = new List&lt;LeaderboardItem&gt;();
  
      var Client = new WebClient();
  
      try
      {
          var Data = Client.DownloadString($"{URL.BaseUrl}/leaderboards/{GameCode}/category/{Category}{Query}");
  
          foreach (var item in (JArray)JObject.Parse(Data)["data"]["runs"])
          {
              int place = (int)item["place"];
  
              // This next line looks up the username on the API.
              // This code takes several seconds per loop to complete.
              // It is returned as a list of strings.
              getNames = new GetNames(item["run"]["players"]); 
  
              double time = (double)item["run"]["times"]["primary_t"];
  
              LBItem = new LeaderboardItem
              {
                  Rank = place,
                  Name = getNames.names,
                  Time = time
              };
  
              LeaderboardList.Add(LBItem);
          }
      }
      catch (Exception err)
      {
          MessageBox.Show($"Error: {err.Message}", "Error!");
      }
  }
  </code></pre>
  
  <p>Each <code>LeaderboardItem</code> looks as follows:</p>
  
  <pre><code>public class LeaderboardItem
  {
      public int Rank { get; set; }
      public List&lt;string&gt; Name { get; set; }
      public double Time { get; set; }
  }
  </code></pre>
  
  <p>My expected result should be that it isn't dependent on each loop to finish before beginning the next username lookup. However, I am inexperienced in multithreading in this manner, so any help would be very much appreciated.</p>
  
  <p>Thank you very much!</p>