下面是示例控制台应用程序,输出是

每次输出是不同的,而且很好,但是在打印结果之前,它需要完成所有的任务。看来,Parallel.ForEachAsync并不是在等待所有任务的完成。我在这里有遗漏什么吗?
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}");
}
}发布于 2022-01-13 22:40:55
回答.NET 6之前的版本。
我觉得你的例子有点让人困惑。这是因为您使用异步回调。异步主要用于IO目的。
要么选择:(这将是CPU绑定的,做一些繁重的计算)。
var responses = new List<string>();
var tests = new List<int>() { 1, 2, 3, 4 ,5,6};
Parallel.ForEach(tests, options, (testno) =>
{
// 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}";
}或go:(这是IO绑定的,例如从外部源获取内容)
var tests = new List<int>() { 1, 2, 3, 4 ,5,6};
var tasks = new List<Task<string>>();
// 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}";
}(可能有一些错误,没有用VS写)
发布于 2022-01-13 20:05:00
答案如下-更改行var responses =新的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}");
}
}https://stackoverflow.com/questions/70702185
复制相似问题