为什么File.ReadAllLinesAsync()阻止UI线程? [英] Why File.ReadAllLinesAsync() blocks the UI thread?

查看:160
本文介绍了为什么File.ReadAllLinesAsync()阻止UI线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码. WPF按钮的事件处理程序,用于读取文件的行:

Here is my code. An event handler for WPF button that reads lines of a file:

private async void Button_OnClick(object sender, RoutedEventArgs e)
{
    Button.Content = "Loading...";
    var lines = await File.ReadAllLinesAsync(@"D:\temp.txt"); //Why blocking UI Thread???
    Button.Content = "Show"; //Reset Button text
}

我在.NET Core 3.1 WPF App中使用了File.ReadAllLines()方法的异步版本.

I used asynchronous version of File.ReadAllLines() method in .NET Core 3.1 WPF App.

但是它阻止了UI线程!为什么?

But it is blocking the UI Thread! Why?

更新:与@Theodor Zoulias一样,我进行了测试:

Update: Same as @Theodor Zoulias, I do a test :

private async void Button_OnClick(object sender, RoutedEventArgs e)
    {
        Button.Content = "Loading...";
        TextBox.Text = "";

        var stopwatch = Stopwatch.StartNew();
        var task = File.ReadAllLinesAsync(@"D:\temp.txt"); //Problem
        var duration1 = stopwatch.ElapsedMilliseconds;
        var isCompleted = task.IsCompleted;
        stopwatch.Restart();
        var lines = await task;
        var duration2 = stopwatch.ElapsedMilliseconds;

        Debug.WriteLine($"Create: {duration1:#,0} msec, Task.IsCompleted: {isCompleted}");
        Debug.WriteLine($"Await:  {duration2:#,0} msec, Lines: {lines.Length:#,0}");


        Button.Content = "Show";
    }

结果是:

Create: 652 msec msec, Task.IsCompleted: False | Await:   15 msec, Lines: 480,001

.NET Core 3.1,C#8,WPF,调试版本| 7.32 Mb文件(.txt)|硬盘5400 SATA

.NET Core 3.1, C# 8, WPF, Debug build | 7.32 Mb File(.txt) | HDD 5400 SATA

推荐答案

遗憾的是,根据Microsoft的

Sadly the built-in asynchronous APIs for accessing the filesystem are not implemented consistently according to Microsoft's own recommendations about how asynchronous methods are expected to behave.

基于TAP的异步方法可以在返回结果任务之前同步完成少量工作,例如验证参数和启动异步操作.同步工作应保持在最低限度,以便异步方法可以快速返回.

An asynchronous method that is based on TAP can do a small amount of work synchronously, such as validating arguments and initiating the asynchronous operation, before it returns the resulting task. Synchronous work should be kept to the minimum so the asynchronous method can return quickly.

StreamReader.ReadToEndAsync 不要以这种方式运行,而是在返回不完整的Task之前,在相当长的时间内阻塞当前线程.例如,在我的较早的实验中,我从中读取了6MB的文件在我的SSD上,此方法将调用线程阻塞了120毫秒,返回了Task,然后仅在20毫秒后完成了该操作.我的建议是避免使用GUI应用程序中的异步文件系统API,而改用

Methods like StreamReader.ReadToEndAsync do not behave this way, and instead block the current thread for a considerable amount of time before returning an incomplete Task. For example in an older experiment of mine with reading a 6MB file from my SSD, this method blocked the calling thread for 120 msec, returning a Task that was then completed after only 20 msec. My suggestion is to avoid using the asynchronous filesystem APIs from GUI applications, and use instead the synchronous APIs wrapped in Task.Run.

var lines = await Task.Run(() => File.ReadAllLines(@"D:\temp.txt"));


更新:这是一些 File.ReadAllLinesAsync :


Update: Here are some experimental results with File.ReadAllLinesAsync:

var stopwatch = Stopwatch.StartNew();
var task = File.ReadAllLinesAsync(@"C:\6MBfile.txt");
var duration1 = stopwatch.ElapsedMilliseconds;
bool isCompleted = task.IsCompleted;
stopwatch.Restart();
var lines = await task;
var duration2 = stopwatch.ElapsedMilliseconds;
Console.WriteLine($"Create: {duration1:#,0} msec, Task.IsCompleted: {isCompleted}");
Console.WriteLine($"Await:  {duration2:#,0} msec, Lines: {lines.Length:#,0}");

输出:

Create: 450 msec, Task.IsCompleted: False
Await:  5 msec, Lines: 204,000

方法File.ReadAllLinesAsync将当前线程阻塞了450毫秒,并且返回的任务在5毫秒后完成.多次运行后,这些测量结果是一致的.

The method File.ReadAllLinesAsync blocked the current thread for 450 msec, and the returned task completed after 5 msec. These measurements are consistent after multiple runs.

.NET Core 3.1.3,C#8,控制台应用,发行版(未连接调试器),Windows 10,SSD东芝OCZ Arc 100 240GB

.NET Core 3.1.3, C# 8, Console App, Release build (no debugger attached), Windows 10, SSD Toshiba OCZ Arc 100 240GB

这篇关于为什么File.ReadAllLinesAsync()阻止UI线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆