I need your help to find the best solution. This is my original code:
public async Task Test()
{
var tasks = new List<Task>();
string line;
using (var streamReader = File.OpenText(InputPath))
{
while ((line = streamReader.ReadLine()) != null)
{
tasks.Add(Process(line));
}
}
await Task.WhenAll(tasks.ToArray());
}
private Task Process(string line)
{
return Task.Run(() =>
{
Console.WriteLine(line);
});
}
It will read a file with lines and process each line by a task. However, if file has more 1 million lines, the array of tasks are bigger, this code is still good? or I should find another solution. Please help me. Thanks.
That's a bad idea. That could launch way too many threads.
A much better way to do this is to simply use
Parallel.ForEach()
like so:This doesn't use async/await, however. But you could wrap the entire call to
Parallel.ForEach()
in a task if you wanted.Alternatively, if you want to use the Task Parallel Library (a Microsoft NuGet package) you can do something like this:
This gives you
async
support.Addendum: A demonstration of threadpool throttling.
(This is in response to shay__'s comments.)
If you start a lot of long-running tasks where the task takes longer to run than a second or so, you may see threadpool throttling.
This happens if the number of threadpool threads for the current process equals or exceeds the
worker
count returned by a call toThreadPool.GetMinThreads(out workers, out ports);
.If this happens, the launching of a new threadpool thread will be delayed by a short while (one second on my system) before a new threadpool thread is created. Often that will have allowed another threadpool thread to become available, and that will be used instead (which of course is a major reason for the throttling).
The following code demonstrates the issue:
On my system, this prints the following:
Note how the first 8 threads start very quickly, but then new threads are throttled to around one per second, until the first batch of threads terminate and then can be reused.
Also note that this effect only occurs if the threads take a relatively long time to terminate.