1

I am trying to get the content length of multiple pages using Task in .NET Core asynchronously:

public static async Task GetContentLengthAsync(string url)
{
    HttpClient httpClient = new HttpClient();
    var httpResponse = await httpClient.GetAsync(url);
    var page = await httpResponse.Content.ReadAsStringAsync();
    Console.WriteLine(page.Length);
}

If I use it like this to take the first 100 pages' content length:

for (int i = 1; i <= 100; i++)
{
    Task.Run(() => GetContentLengthAsync($"https://www.website.com/view/{i}"));
}

=> all the outputs are the same or different but incorrect. (but I get the results very fast)

If I run the Task with await like this by just calling the GetLengthsAsync():

public static async void GetLengthsAsync()
{
    for (int i = 1; i <= 100; i++)
    {

        await Task.Run(() => GetContentLengthAsync($"https://www.website.com/view/{i}"));
    }
}

=> the output is correct and I can still type in the console and do other tasks in that time but each of the GetContentLengthAsync tasks waits the other to be completed and uses only one thread at a time. Is there a way to make it run not only asynchronously but also on multiple threads at the same time without losing information?

P.S. I want to use Tasks because it's an university project and I know that there probably are better ways of doing it(but those are the requirements). It's more of a problem solving task to better understand how Task works.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • As a side note, the `HttpClient` class is intended to be instantiated [once](https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client#create-and-initialize-httpclient), and reused throughout the life of an application. – Theodor Zoulias Aug 18 '21 at 12:57

1 Answers1

2

A lambda function in a for() loop will 'capture' the loop variable. Most requests would have been for /view/100.

for (int i = 1; i <= 100; i++)
{
   int localCopy = i;
   Task.Run(() => GetContentLengthAsync($"https://www.website.com/view/{localCopy}"));
}
H H
  • 263,252
  • 30
  • 330
  • 514