FileStream.ReadAsync(Byte[], Int32, Int32, CancellationToken) FileStream.ReadAsync(Byte[], Int32, Int32, CancellationToken) FileStream.ReadAsync(Byte[], Int32, Int32, CancellationToken) Method


Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

 override System::Threading::Tasks::Task<int> ^ ReadAsync(cli::array <System::Byte> ^ buffer, int offset, int count, System::Threading::CancellationToken cancellationToken);
public override System.Threading.Tasks.Task<int> ReadAsync (byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken);
override this.ReadAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>



The buffer to write the data into.

Int32 Int32 Int32

The byte offset in buffer at which to begin writing data from the stream.

Int32 Int32 Int32

The maximum number of bytes to read.

CancellationToken CancellationToken CancellationToken

The token to monitor for cancellation requests.


A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.


The sum of offset and count is larger than the buffer length.

The stream is currently in use by a previous read operation.


The following example shows how to read from a file asynchronously.

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()

        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


The ReadAsync method enables you to perform resource-intensive file operations without blocking the main thread. This performance consideration is particularly important in a Windows 8.x Store app or desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working. The async methods are used in conjunction with the async and await keywords in Visual Basic and C#.

Use the CanRead property to determine whether the current instance supports reading.

If the operation is canceled before it completes, the returned task contains the Canceled value for the Status property. If the handle to the file is disposed, the returned task contains the ObjectDisposedException exception in the Exception property.

Applies to

See also