StreamReader等待ReadLineAsync阻止执行

我在桌面应用程序中有以下代码:

using (StreamReader reader = new StreamReader("path\\to\\file.csv"))
{
    string line;
    while (!reader.EndOfStream)
    {
        line = await reader.ReadLineAsync();
        string[] values = line.Split(',');
        double.TryParse(values[pointsIndex], out double points);
    }
}

The line line = await reader.ReadLineAsync() blocks the the program entirely and makes the application unresponsive. While debugging I noticed that it is runs 4 times and then blocks indefinitely.

这段代码非常简单,正确的AFAIK。为什么它没有按预期工作?

评论
  • quia_a
    quia_a 回复

    尝试将其更改为

    using (StreamReader reader = new StreamReader("path\\to\\file.csv"))
    {
        string line;
        while ((line = await reader.ReadLineAsync()) != null)
        {
         string[] values = line.Split(',');
         double.TryParse(values[pointsIndex], out double points);
        }
    }
    

    当它结束时-结果将为null并终止。