title: Using parallelism to speed up the code
body: <p>I've made a tool that sends HTTP requests (GET) (reads the info of what to send from a .txt), captures the json and parses it + writes it to a .txt. The tool is a console app targetting .NET Framework 4.5.</p>
  
  <p>Could I possibly speed this up with the help of "multi-threading"?</p>
  
  <pre class="lang-cs prettyprint-override"><code>        System.IO.StreamReader file =
              new System.IO.StreamReader(@"file.txt");
          while ((line = file.ReadLine()) != null)
          {
              using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
              {
                  client.BaseAddress = new Uri("https://www.website.com/");
                  HttpResponseMessage response = client.GetAsync("index?userGetLevel=" + line).Result;
                  response.EnsureSuccessStatusCode();
                  string result = response.Content.ReadAsStringAsync().Result;
                  dynamic json = JObject.Parse(result);
                  string level = json.data.level;
  
                  if (level == "null")
                  {
                      Console.WriteLine("{0}: User does not exist.", line);
                  }
                  else
                  {
                      Console.WriteLine("{0}: User level is {1}.", line, level);
  
                      using (StreamWriter sw = File.AppendText(levels.txt))
                      {
                          sw.WriteLine("{0} : {1}", line, level);
                      }
                  }
  
              }
          }
  
          file.Close();
  </code></pre>
  
  <hr>
  
  <p><strong>Answers to questions:</strong></p>
  
  <p><strong>"Speed up what?":</strong> I'd like to speed up the whole process (the amount of requests it sends each time, not how fast it sends them.</p>
  
  <p><strong>"How many requests?":</strong> The tool reads a string from a text file and puts that information in to a part of the URL, and then captures the response, then places that in a result.txt. I'd like to increase the speed it does this at/how many it does at a time.</p>
  
  <p><strong>"Can the requests happen concurrently or are dependant on prior request responses?":</strong> Yes, and no, they are not dependent.</p>
  
  <p><strong>" Does your web server impose a limit on the number of concurrent requests?":</strong> No.</p>
  
  <p><strong>"How long does a typical request take?":</strong> The request + the time the response shows up on console per each requests is a little bit more than 1/3 of a second.</p>