I have gone through many Stackoverflow threads but I am still not sure why this is taking so long. I am using VS 2022 and it's a console app and the target is .NET 6.0. There is just one file Program.cs where the function and the call to the function is coded.
I am making a GET call to an external API. Since that API returns 10000+ rows, I am trying to call my method that calls this API, 2-3 times in Parallel. I also try to update this Concurrent dictionary object that is declared at the top, which I then use LINQ to show some summaries on the UI.
This same external GET call on Postman takes less than 30 seconds but my app takes for ever.
Here is my code. Right now this entire code is in Program.cs of a Console application. I plan to move the GetAPIData() method to a class library after this works.
static async Task GetAPIData(string url, int taskNumber)
{
var client = new HttpClient();
var serializer = new JsonSerializer();
client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
var bearerToken = "xxxxxxxxxxxxxx";
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {bearerToken}");
using (var stream = await client.GetStreamAsync(url).ConfigureAwait(false))
using (var sr = new StreamReader(stream))
using (JsonTextReader reader = new JsonTextReader(sr))
{
reader.SupportMultipleContent = true;
while (reader.Read())
{
if (reader.TokenType == JsonToken.StartObject)
{
bag.Add(serializer.Deserialize<Stats?>(reader));
}
}
}
}
My calling code:
var taskNumber=1;
var url = "https://extrenalsite/api/stream";
var bag = ConcurrentBag<Stats>();
var task1 = Task.Run(() => GetAPIData(url, bag, taskNumber++));
var task2 = Task.Run(() => GetAPIData(url, bag, taskNumber++));
var task3 = Task.Run(() => GetAPIData(url, bag, taskNumber++));
await Task.WhenAll(task1, task2, task3);
Please let me know why it is taking way too long to execute when I have spawned 3 threads and why it's slow.
Thanks.
>` instead of directly writing to `bag` and merge the three lists after awaiting all task.
– Klaus Gütter Feb 05 '23 at 06:32>. For one Task, it works good. 2 it runs forever.
– hillcountry99 Feb 05 '23 at 06:52