public ReadAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task |
||
buffer | byte | |
offset | int | |
count | int | |
cancellationToken | CancellationToken | |
return | Task |
using System; using System.IO; using System.Text; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { using (FileStream sourceStream = new FileStream("file.txt", FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true)) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0) { Console.WriteLine($"Read {numRead} bytes."); // Process the buffer } } } }
using System; using System.IO; using System.Net.Sockets; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { TcpClient client = new TcpClient(); await client.ConnectAsync("www.example.com", 80); using (Stream networkStream = client.GetStream()) { byte[] buffer = new byte[0x1000]; int numRead; do { numRead = await networkStream.ReadAsync(buffer, 0, buffer.Length); Console.WriteLine($"Read {numRead} bytes."); // Process the buffer } while (numRead != 0); } } }In this example, a TcpClient is created to connect to www.example.com on port 80. The GetStream method returns the network stream used to read/write data from/to the server. The while loop reads data from the network stream asynchronously using the ReadAsync method until all data is read from the stream. Package Library: System.Runtime.Extensions.
public ReadAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task |
||
buffer | byte | |
offset | int | |
count | int | |
cancellationToken | CancellationToken | |
return | Task |