Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FileStream.EndRead Method

Waits for the pending asynchronous read to complete.

[Visual Basic]
Overrides Public Function EndRead( _
   ByVal asyncResult As IAsyncResult _
) As Integer
[C#]
public override int EndRead(
 IAsyncResult asyncResult
);
[C++]
public: int EndRead(
 IAsyncResult* asyncResult
);
[JScript]
public override function EndRead(
   asyncResult : IAsyncResult
) : int;

Parameters

asyncResult
The reference to the pending asynchronous request to wait for.

Return Value

The number of bytes read from the stream, between 0 and the number of bytes you requested. Streams only return 0 at the end of the stream, otherwise, they should block until at least 1 byte is available.

Exceptions

Exception Type Condition
ArgumentNullException asyncResult is a null reference (Nothing in Visual Basic).
ArgumentException This IAsyncResult object was not created by calling BeginRead on this class.
InvalidOperationException EndRead is called multiple times.

Remarks

This method overrides EndRead.

EndRead can be called on every IAsyncResult from BeginRead. Calling EndRead tells you how many bytes were read from the stream. EndRead will block until the I/O operation has completed.

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Rename or move a file. File.Move

FileInfo.MoveTo

Copy a file. File.Copy

FileInfo.CopyTo

Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File
Create a directory. Directory.CreateDirectory

Directory.CreateDirectory

Example

[Visual Basic, C#, C++] This code example is part of a larger example provided for System.IO.FileStream.FileStream4.

[Visual Basic] 
Private Shared Sub EndReadCallback(asyncResult As IAsyncResult)
     Dim tempState As State = _
         DirectCast(asyncResult.AsyncState, State)
     Dim readCount As Integer = _
         tempState.FStream.EndRead(asyncResult)

     Dim i As Integer = 0
     While(i < readCount)
         If(tempState.ReadArray(i) <> tempState.WriteArray(i))
             Console.WriteLine("Error writing data.")
             tempState.FStream.Close()
             Return
         End If
         i += 1
     End While

     Console.WriteLine("The data was written to {0} and " & _
         "verified.", tempState.FStream.Name)
     tempState.FStream.Close()

     ' Signal the main thread that the verification is finished.
     tempState.ManualEvent.Set()
 End Sub

[C#] 
static void EndReadCallback(IAsyncResult asyncResult)
{
    State tempState = (State)asyncResult.AsyncState;
    int readCount = tempState.FStream.EndRead(asyncResult);

    int i = 0;
    while(i < readCount)
    {
        if(tempState.ReadArray[i] != tempState.WriteArray[i++])
        {
            Console.WriteLine("Error writing data.");
            tempState.FStream.Close();
            return;
        }
    }
    Console.WriteLine("The data was written to {0} and verified.",
        tempState.FStream.Name);
    tempState.FStream.Close();

    // Signal the main thread that the verification is finished.
    tempState.ManualEvent.Set();
}

[C++] 
static void EndReadCallback(IAsyncResult* asyncResult)
{
    State* tempState = 
        dynamic_cast<State*>(asyncResult->AsyncState);
    int readCount = tempState->FStream->EndRead(asyncResult);

    int i = 0;
    while(i < readCount)
    {
        if(tempState->ReadArray[i] != tempState->WriteArray[i++])
        {
            Console::WriteLine(S"Error writing data.");
            tempState->FStream->Close();
            return;
        }
    }
    Console::WriteLine(S"The data was written to {0} "
        S"and verified.", tempState->FStream->Name);
    tempState->FStream->Close();

    // Signal the main thread that the verification is finished.
    tempState->ManualEvent->Set();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

FileStream Class | FileStream Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File | Asynchronous File I/O

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft