异步函数阻塞 UI 线程中的 IOException

IOException in Async Function Blocking UI Thread

使用以下代码,如果 "The network path was not found" 它会完全阻止 UI 长达 15 秒。如果我用 Await Task.Delay(5000) 替换代码,它不会。这就像打开 FileStream 不是异步发生的...

如何在不阻塞 UI 的情况下处理这种情况?

有关信息,我正在尝试异步读取一行 (1kb) 文本文件。

Private Async Function getDataAsync(filepath As String, ct As CancellationToken) As Task(Of String)
    Dim data as string
    Try
        Using sourceStream As New FileStream(filepath, FileMode.Open, FileAccess.Read,
                                             FileShare.Read, bufferSize:=4096, useAsync:=True)
            Dim reader As New StreamReader(sourceStream)
            data = Await reader.ReadLineAsync()
        End Using
    Catch ex As Exception
        data = ex.Message
    End Try

    Return data
End Function

问题是执行始终是同步的,直到第一个 Await

尝试这样的事情:

Private Async Function getDataAsync(filepath As String, ct As CancellationToken) As Task(Of String)
    Return Await Task.Run(Function()
        Dim data as string
        Try
            Using sourceStream As New FileStream(filepath, FileMode.Open, FileAccess.Read,
                                                 FileShare.Read, bufferSize:=4096, useAsync:=True)
                Dim reader As New StreamReader(sourceStream)
                data = Await reader.ReadLineAsync()
            End Using
        Catch ex As Exception
            data = ex.Message
        End Try
        Return data
    End Function)
End Function