Stream.ReadAsync 方法

定义

从当前流异步读取字节序列,并将流中的位置提升读取的字节数。

重载

ReadAsync(Memory<Byte>, CancellationToken)

从当前流异步读取字节的序列,将流中的位置提升读取的字节数,并监视取消请求。

ReadAsync(Byte[], Int32, Int32)

从当前流异步读取字节序列,并将流中的位置提升读取的字节数。

ReadAsync(Byte[], Int32, Int32, CancellationToken)

从当前流异步读取字节的序列,将流中的位置提升读取的字节数,并监视取消请求。

ReadAsync(Memory<Byte>, CancellationToken)

Source:
Stream.cs
Source:
Stream.cs
Source:
Stream.cs

从当前流异步读取字节的序列,将流中的位置提升读取的字节数,并监视取消请求。

public virtual System.Threading.Tasks.ValueTask<int> ReadAsync (Memory<byte> buffer, System.Threading.CancellationToken cancellationToken = default);
abstract member ReadAsync : Memory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
override this.ReadAsync : Memory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Overridable Function ReadAsync (buffer As Memory(Of Byte), Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)

参数

buffer
Memory<Byte>

要将数据写入的内存区域。

cancellationToken
CancellationToken

要监视取消请求的标记。 默认值为 None

返回

表示异步读取操作的任务。 其 Result 属性的值包含读入缓冲区的总字节数。 如果当前没有可用的字节数,则结果值可能小于缓冲区的长度;如果缓冲区的长度为 0,则结果值可以是 0 (零) 如果已到达流的末尾。

例外

取消令牌已取消。 此异常存储在返回的任务中。

注解

使用 ReadAsync 方法可以执行资源密集型 I/O 操作,而无需阻止main线程。 在 Windows 8.x 应用商店应用或桌面应用中一个耗时的流操作可能阻塞 UI 线程并让应用看起来好像不工作时,这种性能的考虑就显得尤为重要了。 异步方法与 async Visual Basic 和 C# 中的 和 await 关键字结合使用。

CanRead使用 属性确定当前实例是否支持读取。

如果在操作完成之前取消,则返回的任务将 TaskStatus.Canceled 包含 属性的值 Status

有关示例,请参阅 ReadAsync(Byte[], Int32, Int32) 重载。

适用于

ReadAsync(Byte[], Int32, Int32)

Source:
Stream.cs
Source:
Stream.cs
Source:
Stream.cs

从当前流异步读取字节序列,并将流中的位置提升读取的字节数。

public:
 System::Threading::Tasks::Task<int> ^ ReadAsync(cli::array <System::Byte> ^ buffer, int offset, int count);
public System.Threading.Tasks.Task<int> ReadAsync (byte[] buffer, int offset, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public System.Threading.Tasks.Task<int> ReadAsync (byte[] buffer, int offset, int count);
member this.ReadAsync : byte[] * int * int -> System.Threading.Tasks.Task<int>
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.ReadAsync : byte[] * int * int -> System.Threading.Tasks.Task<int>
Public Function ReadAsync (buffer As Byte(), offset As Integer, count As Integer) As Task(Of Integer)

参数

buffer
Byte[]

要写入数据的缓冲区。

offset
Int32

buffer 中的字节偏移量,从该偏移量开始写入从流中读取的数据。

count
Int32

最多读取的字节数。

返回

表示异步读取操作的任务。 TResult 参数的值包含读入缓冲区的总字节数。 如果当前可用的字节数小于请求的数目,则结果值可能小于请求的字节数;如果 为 0,则结果值可以是 0 (零) 如果 count 为 0 或已到达流的末尾。

属性

例外

buffernull

offsetcount 为负数。

offsetcount 的总和大于缓冲区长度。

流不支持读取。

已释放流。

之前的读取操作当前正在使用流。

示例

以下示例演示如何异步读取文件。 该示例使用 FileStream 派生自 类的 Stream 类。

using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.IO;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            string filename = @"c:\Temp\userinputlog.txt";
            byte[] result;

            using (FileStream SourceStream = File.Open(filename, FileMode.Open))
            {
                result = new byte[SourceStream.Length];
                await SourceStream.ReadAsync(result, 0, (int)SourceStream.Length);
            }

            UserInput.Text = System.Text.Encoding.ASCII.GetString(result);
        }
    }
}
Imports System.IO
Imports System.Text

Class MainWindow
    Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim filename As String = "c:\Temp\userinputlog.txt"

        Dim result As Byte()

        Using SourceStream As FileStream = File.Open(filename, FileMode.Open)
            result = New Byte(SourceStream.Length - 1) {}
            Await SourceStream.ReadAsync(result, 0, CType(SourceStream.Length, Integer))
        End Using

        UserInput.Text = System.Text.Encoding.ASCII.GetString(result)
    End Sub
End Class

注解

使用 ReadAsync 方法可以执行资源密集型 I/O 操作,而无需阻止main线程。 在 Windows 8.x 应用商店应用或桌面应用中一个耗时的流操作可能阻塞 UI 线程并让应用看起来好像不工作时,这种性能的考虑就显得尤为重要了。 异步方法与 async Visual Basic 和 C# 中的 和 await 关键字结合使用。

CanRead使用 属性确定当前实例是否支持读取。

此方法存储在任务中,它返回该方法的同步对应项可能引发的所有非使用异常。 如果异常存储在返回的任务中,则在等待任务时将引发该异常。 使用异常(如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅 引发的 Read(Byte[], Int32, Int32)异常。

适用于

ReadAsync(Byte[], Int32, Int32, CancellationToken)

Source:
Stream.cs
Source:
Stream.cs
Source:
Stream.cs

从当前流异步读取字节的序列,将流中的位置提升读取的字节数,并监视取消请求。

public:
 virtual System::Threading::Tasks::Task<int> ^ ReadAsync(cli::array <System::Byte> ^ buffer, int offset, int count, System::Threading::CancellationToken cancellationToken);
public virtual System.Threading.Tasks.Task<int> ReadAsync (byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken);
[System.Runtime.InteropServices.ComVisible(false)]
public virtual System.Threading.Tasks.Task<int> ReadAsync (byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken);
abstract member ReadAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>
override this.ReadAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member ReadAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>
override this.ReadAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>
Public Overridable Function ReadAsync (buffer As Byte(), offset As Integer, count As Integer, cancellationToken As CancellationToken) As Task(Of Integer)

参数

buffer
Byte[]

要写入数据的缓冲区。

offset
Int32

buffer 中的字节偏移量,从该偏移量开始写入从流中读取的数据。

count
Int32

最多读取的字节数。

cancellationToken
CancellationToken

要监视取消请求的标记。 默认值为 None

返回

表示异步读取操作的任务。 TResult 参数的值包含读入缓冲区的总字节数。 如果当前可用的字节数小于请求的数目,则结果值可能小于请求的字节数;如果 为 0,则结果值可以是 0 (零) 如果 count 为 0 或已到达流的末尾。

属性

例外

buffernull

offsetcount 为负数。

offsetcount 的总和大于缓冲区长度。

流不支持读取。

已释放流。

之前的读取操作当前正在使用流。

取消令牌已取消。 此异常存储在返回的任务中。

注解

使用 ReadAsync 方法可以执行资源密集型 I/O 操作,而无需阻止main线程。 在 Windows 8.x 应用商店应用或桌面应用中一个耗时的流操作可能阻塞 UI 线程并让应用看起来好像不工作时,这种性能的考虑就显得尤为重要了。 异步方法与 async Visual Basic 和 C# 中的 和 await 关键字结合使用。

CanRead使用 属性确定当前实例是否支持读取。

如果在操作完成之前取消,则返回的任务将 Canceled 包含 属性的值 Status

有关示例,请参阅 ReadAsync(Byte[], Int32, Int32) 重载。

此方法存储在任务中,它返回该方法的同步对应项可能引发的所有非使用异常。 如果异常存储在返回的任务中,则在等待任务时将引发该异常。 使用异常(如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅 引发的 Read(Byte[], Int32, Int32)异常。

另请参阅

适用于