Skip to main content
.NET Framework Class Library
StreamReadAsync Method (Byte, Int32, Int32)

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

Namespace:   System.IO
Assemblies:   mscorlib (in mscorlib.dll)
  System.IO (in System.IO.dll)
Syntax
<[%$TOPIC/hh137813_en-us_VS_110_1_0_0_0_0%](False)> _
<[%$TOPIC/hh137813_en-us_VS_110_1_0_0_0_1%]([%$TOPIC/hh137813_en-us_VS_110_1_0_0_0_2%].LinkDemand, ExternalThreading := True)> _
Public Function ReadAsync ( _
	buffer As [%$TOPIC/hh137813_en-us_VS_110_1_0_0_0_3%](), _
	offset As [%$TOPIC/hh137813_en-us_VS_110_1_0_0_0_4%], _
	count As [%$TOPIC/hh137813_en-us_VS_110_1_0_0_0_5%] _
) As [%$TOPIC/hh137813_en-us_VS_110_1_0_0_0_6%](Of [%$TOPIC/hh137813_en-us_VS_110_1_0_0_0_7%])
[[%$TOPIC/hh137813_en-us_VS_110_1_0_1_0_0%](false)]
[[%$TOPIC/hh137813_en-us_VS_110_1_0_1_0_1%]([%$TOPIC/hh137813_en-us_VS_110_1_0_1_0_2%].LinkDemand, ExternalThreading = true)]
public [%$TOPIC/hh137813_en-us_VS_110_1_0_1_0_3%]<[%$TOPIC/hh137813_en-us_VS_110_1_0_1_0_4%]> ReadAsync(
	[%$TOPIC/hh137813_en-us_VS_110_1_0_1_0_5%][] buffer,
	[%$TOPIC/hh137813_en-us_VS_110_1_0_1_0_6%] offset,
	[%$TOPIC/hh137813_en-us_VS_110_1_0_1_0_7%] count
)
[[%$TOPIC/hh137813_en-us_VS_110_1_0_2_0_0%](false)]
[[%$TOPIC/hh137813_en-us_VS_110_1_0_2_0_1%]([%$TOPIC/hh137813_en-us_VS_110_1_0_2_0_2%]::LinkDemand, ExternalThreading = true)]
public:
[%$TOPIC/hh137813_en-us_VS_110_1_0_2_0_3%]<[%$TOPIC/hh137813_en-us_VS_110_1_0_2_0_4%]>^ ReadAsync(
	array<[%$TOPIC/hh137813_en-us_VS_110_1_0_2_0_5%]>^ buffer, 
	[%$TOPIC/hh137813_en-us_VS_110_1_0_2_0_6%] offset, 
	[%$TOPIC/hh137813_en-us_VS_110_1_0_2_0_7%] count
)
[<[%$TOPIC/hh137813_en-us_VS_110_1_0_3_0_0%](false)>]
[<[%$TOPIC/hh137813_en-us_VS_110_1_0_3_0_1%]([%$TOPIC/hh137813_en-us_VS_110_1_0_3_0_2%].LinkDemand, ExternalThreading = true)>]
member ReadAsync : 
        buffer:[%$TOPIC/hh137813_en-us_VS_110_1_0_3_0_3%][] * 
        offset:[%$TOPIC/hh137813_en-us_VS_110_1_0_3_0_4%] * 
        count:[%$TOPIC/hh137813_en-us_VS_110_1_0_3_0_5%] -> [%$TOPIC/hh137813_en-us_VS_110_1_0_3_0_6%]<[%$TOPIC/hh137813_en-us_VS_110_1_0_3_0_7%]> 
public function ReadAsync(
	buffer : [%$TOPIC/hh137813_en-us_VS_110_1_0_4_0_0%][], 
	offset : [%$TOPIC/hh137813_en-us_VS_110_1_0_4_0_1%], 
	count : [%$TOPIC/hh137813_en-us_VS_110_1_0_4_0_2%]
) : [%$TOPIC/hh137813_en-us_VS_110_1_0_4_0_3%]<[%$TOPIC/hh137813_en-us_VS_110_1_0_4_0_4%]>

Parameters

buffer
Type: SystemByte

The buffer to write the data into.

offset
Type: SystemInt32

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

count
Type: SystemInt32

The maximum number of bytes to read.

Return Value

Type: System.Threading.TasksTask Int32
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.
Exceptions
ExceptionCondition
ArgumentNullException

buffer is .

ArgumentOutOfRangeException

offset or count is negative.

ArgumentException

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

NotSupportedException

The stream does not support reading.

ObjectDisposedException

The stream has been disposed.

InvalidOperationException

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

Remarks

The ReadAsync method enables you to perform resource-intensive I/O operations without blocking the main thread. This performance consideration is particularly important in a Windows 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.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

Examples

The following example shows how to read from a file asynchronously. The example uses the FileStream class, which derives from the Stream class.

Imports System.IO
Imports System.Text

Class MainWindow
    Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim uniencoding As UnicodeEncoding = New UnicodeEncoding()
        Dim filename As String = "c:\Users\exampleuser\Documents\userinputlog.txt" 

        Dim result As Byte()

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

        UserInput.Text = uniencoding.GetString(result)
    End Sub 
End Class
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)
        {
            UnicodeEncoding uniencoding = new UnicodeEncoding();
            string filename = @"c:\Users\exampleuser\Documents\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 = uniencoding.GetString(result);
        }
    }
}
Version Information

.NET Framework

Supported in: 4.6, 4.5

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8