FileStream.ReadAsync メソッド
定義
オーバーロード
ReadAsync(Memory<Byte>, CancellationToken) |
バイト シーケンスの現在のファイル ストリームからの読み取りと、メモリ領域への書き込みを非同期に行い、読み取られたバイト数だけファイル ストリーム内の位置を進め、キャンセル要求を監視します。Asynchronously reads a sequence of bytes from the current file stream and writes them to a memory region, advances the position within the file stream by the number of bytes read, and monitors cancellation requests. |
ReadAsync(Byte[], Int32, Int32, CancellationToken) |
バイト シーケンスの現在のファイル ストリームからの読み取りと、指定されたオフセットで開始されるバイト配列への書き込みを非同期に行い、読み取られたバイトの合計数だけファイル ストリーム内の位置を進め、キャンセル要求を監視します。Asynchronously reads a sequence of bytes from the current file stream and writes them to a byte array beginning at a specified offset, advances the position within the file stream by the number of bytes read, and monitors cancellation requests. |
ReadAsync(Memory<Byte>, CancellationToken)
バイト シーケンスの現在のファイル ストリームからの読み取りと、メモリ領域への書き込みを非同期に行い、読み取られたバイト数だけファイル ストリーム内の位置を進め、キャンセル要求を監視します。Asynchronously reads a sequence of bytes from the current file stream and writes them to a memory region, advances the position within the file stream by the number of bytes read, and monitors cancellation requests.
public override System.Threading.Tasks.ValueTask<int> ReadAsync (Memory<byte> buffer, System.Threading.CancellationToken cancellationToken = default);
override this.ReadAsync : Memory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Overrides Function ReadAsync (buffer As Memory(Of Byte), Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)
パラメーター
- cancellationToken
- CancellationToken
キャンセル要求を監視するためのトークン。The token to monitor for cancellation requests. 既定値は None です。The default value is None.
戻り値
非同期の読み取り操作を表すタスク。バッファーへ読み取られたバイトの合計数がラップされます。A task that represents the asynchronous read operation and wraps the total number of bytes read into the buffer. 現在使用できるバイト数が要求した数より小さい場合、結果の値は要求したバイト数より小さくなることがあります。また、ストリームの末尾に到達した場合は 0 になることがあります。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 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()
{
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
メソッドを使用すると、メインスレッドをブロックせずに、リソースを集中的に使用するファイル操作を実行できます。The ReadAsync
method enables you to perform resource-intensive file operations without blocking the main thread. このパフォーマンスに関する考慮事項は、時間のかかるストリーム操作によって UI スレッドがブロックされ、アプリが動作していないと見なされる可能性がある Windows 8.x ストアWindows 8.x Store アプリまたは デスクトップdesktop アプリで特に重要です。This performance consideration is particularly important in a Windows 8.x ストア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. 非同期メソッドは、 async
Visual Basic および C# のキーワードとキーワードと組み合わせて使用され await
ます。The async methods are used in conjunction with the async
and await
keywords in Visual Basic and C#.
CanRead現在のインスタンスが読み取りをサポートしているかどうかを確認するには、プロパティを使用します。Use the CanRead property to determine whether the current instance supports reading.
操作が完了前にキャンセルされた場合、返されたタスクには TaskStatus.Canceled プロパティの値が含まれ Status ます。If the operation is canceled before it completes, the returned task contains the TaskStatus.Canceled value for the Status property. ファイルへのハンドルが破棄された場合、返されたタスクにはプロパティの例外が含まれ ObjectDisposedException Task.Exception ます。If the handle to the file is disposed, the returned task contains the ObjectDisposedException exception in the Task.Exception property.
適用対象
ReadAsync(Byte[], Int32, Int32, CancellationToken)
バイト シーケンスの現在のファイル ストリームからの読み取りと、指定されたオフセットで開始されるバイト配列への書き込みを非同期に行い、読み取られたバイトの合計数だけファイル ストリーム内の位置を進め、キャンセル要求を監視します。Asynchronously reads a sequence of bytes from the current file stream and writes them to a byte array beginning at a specified offset, advances the position within the file stream by the number of bytes read, and monitors cancellation requests.
public:
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);
[System.Runtime.InteropServices.ComVisible(false)]
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>
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.ReadAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>
Public Overrides Function ReadAsync (buffer As Byte(), offset As Integer, count As Integer, cancellationToken As CancellationToken) As Task(Of Integer)
パラメーター
- buffer
- Byte[]
データを書き込むバッファー。The buffer to write the data into.
- offset
- Int32
ストリームからのデータの書き込み開始位置を示す buffer
内のバイト オフセット。The byte offset in buffer
at which to begin writing data from the stream.
- count
- Int32
読み取る最大バイト数。The maximum number of bytes to read.
- cancellationToken
- CancellationToken
キャンセル要求を監視するためのトークン。The token to monitor for cancellation requests.
戻り値
非同期の読み取り操作を表すタスク。バッファーへ読み取られたバイトの合計数がラップされます。A task that represents the asynchronous read operation and wraps the total number of bytes read into the buffer. 現在使用できるバイト数が要求した数より小さい場合、結果の値は要求したバイト数より小さくなることがあります。また、ストリームの末尾に到達した場合は 0 になることがあります。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.
- 属性
例外
buffer
が null
です。buffer
is null
.
offset
または count
が負の値です。offset
or count
is negative.
offset
と count
の合計が、バッファーの長さよりも大きいです。The sum of offset
and count
is larger than the buffer length.
ストリームは読み取りをサポートしません。The stream does not support reading.
ストリームは破棄されています。The stream has been disposed.
ストリームは、前の読み取り操作によって現在使用されています。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()
{
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
メソッドを使用すると、メインスレッドをブロックせずに、リソースを集中的に使用するファイル操作を実行できます。The ReadAsync
method enables you to perform resource-intensive file operations without blocking the main thread. このパフォーマンスに関する考慮事項は、時間のかかるストリーム操作によって UI スレッドがブロックされ、アプリが動作していないと見なされる可能性がある Windows 8.x ストアWindows 8.x Store アプリまたは デスクトップdesktop アプリで特に重要です。This performance consideration is particularly important in a Windows 8.x ストア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. 非同期メソッドは、 async
Visual Basic および C# のキーワードとキーワードと組み合わせて使用され await
ます。The async methods are used in conjunction with the async
and await
keywords in Visual Basic and C#.
CanRead現在のインスタンスが読み取りをサポートしているかどうかを確認するには、プロパティを使用します。Use the CanRead property to determine whether the current instance supports reading.
操作が完了前にキャンセルされた場合、返されたタスクには TaskStatus.Canceled プロパティの値が含まれ Task.Status ます。If the operation is canceled before it completes, the returned task contains the TaskStatus.Canceled value for the Task.Status property. ファイルへのハンドルが破棄された場合、返されたタスクにはプロパティの例外が含まれ ObjectDisposedException Task.Exception ます。If the handle to the file is disposed, the returned task contains the ObjectDisposedException exception in the Task.Exception property.