public BeginRead ( byte array, int offset, int numBytes, AsyncCallback callback, object state ) : IAsyncResult | ||
array | byte | |
offset | int | |
numBytes | int | |
callback | AsyncCallback | |
state | object | |
return | IAsyncResult |
FileStream fileStream = new FileStream("file.txt", FileMode.Open, FileAccess.Read); byte[] buffer = new byte[1024]; IAsyncResult asyncResult = fileStream.BeginRead(buffer, 0, 1024, null, null); // Do other work while the read operation is in progress int bytesRead = fileStream.EndRead(asyncResult);
FileStream fileStream = new FileStream("file.txt", FileMode.Open, FileAccess.Read); byte[] buffer = new byte[1024]; IAsyncResult asyncResult = fileStream.BeginRead(buffer, 0, 1024, null, null); while (!asyncResult.IsCompleted) { // Do other work while the read operation is in progress } int bytesRead = fileStream.EndRead(asyncResult);This example is similar to the previous one, but instead of calling EndRead immediately after BeginRead, it uses a loop to wait for the read operation to complete. This approach can be useful when the size of the data to be read is not known in advance. The FileStream BeginRead method is part of the System.IO package library in C#.
public BeginRead ( byte array, int offset, int numBytes, AsyncCallback callback, object state ) : IAsyncResult | ||
array | byte | |
offset | int | |
numBytes | int | |
callback | AsyncCallback | |
state | object | |
return | IAsyncResult |