Below is sample console app and output is
Output is different each time and is fine but it needs to complete all tasks before I print result.
It seems that Parallel.ForEachAsync is not waiting for all tasks to be completed. Am I missing anything here ?
internal class Program
{
private async static Task Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
await TestParallel();
sw.Stop();
Console.WriteLine("Elapsed={0}", sw.Elapsed);
Console.ReadLine();
}
private static async Task TestParallel()
{
var tests = new List<int>() { 1, 2, 3, 4, 5, 6 };
var options = new ParallelOptions { MaxDegreeOfParallelism = 5,
CancellationToken = CancellationToken.None };
var responses = new List<string>();
await Parallel.ForEachAsync(tests, options, async (testno, cancellationToken) =>
{
var response = await TestTask(testno);
responses.Add(response);
});
foreach (var response in responses)
{
Console.WriteLine(response);
}
}
private static Task<string> TestTask(int testno)
{
System.Threading.Thread.Sleep(1000);
return Task.FromResult($"Test{testno}");
}
}
CodePudding user response:
Answer is below - changed line var responses = new ConcurrentBag();
internal class Program
{
private async static Task Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
await TestParallel();
sw.Stop();
Console.WriteLine("Elapsed={0}", sw.Elapsed);
Console.ReadLine();
}
private static async Task TestParallel()
{
var tests = new List<int>() { 1, 2, 3, 4 ,5,6};
var options = new ParallelOptions { MaxDegreeOfParallelism = 5, CancellationToken = CancellationToken.None };
var responses = new ConcurrentBag<string>();
await Parallel.ForEachAsync(tests, options, async (testno, cancellationToken) =>
{
var response = await TestTask(testno);
responses.Add(response);
});
foreach (var response in responses)
{
Console.WriteLine(response);
}
}
private static Task<string> TestTask(int testno)
{
System.Threading.Thread.Sleep(1000);
return Task.FromResult($"Test{testno}");
}
}
CodePudding user response:
I think your example is a bit confusing. That's because you use an async callback. Mostly async is used for IO purposes.
Either go for: (this would be CPU-bound, doing some heavy calculations)
var responses = new List<string>();
var tests = new List<int>() { 1, 2, 3, 4 ,5,6};
await Parallel.ForEachAsync(tests, options, (testno, cancellationToken) =>
{
// no async here...
var response = TestTask(testno);
// lock the shared resource.
lock(responses)
responses.Add(response);
});
foreach (var response in responses)
{
Console.WriteLine(response);
}
private static string TestTask(int testno)
{
// calculations done here
System.Threading.Thread.Sleep(1000);
return $"Test{testno}";
}
The ForEachAsync is useful when you start CPU-bound work from a UI-thread.
Or go for: (this is IO-bound, for example getting content from external sources)
var tests = new List<int>() { 1, 2, 3, 4 ,5,6};
var tasks = new List<Task>();
// just add the tasks to a list, so you can await them later.
// the first part (till the first await) will be completed synchronous.
// If any async/await is used, the Task.WhenAll will wait for it.
// Multiple tasks can be running simultaneously.
foreach(var t in tests)
tasks.Add(TestTask(t));
await Task.WhenAll(tasks);
foreach (var task in tasks)
{
// the current thread won't be blocked by calling the .Result here
// All tasks are already completed.
Console.WriteLine(task.Result);
}
private static async Task<string> TestTask(int testno)
{
// Getting information from external resources.
await Task.Delay(1000);
return $"Test{testno}";
}
(there might be some typo's, haven't written in VS)

