FileStream.EndRead Method
Assembly: mscorlib (in mscorlib.dll)
public int EndRead ( IAsyncResult asyncResult )
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.| Exception type | Condition |
|---|---|
| asyncResult is a null reference (Nothing in Visual Basic). | |
| This IAsyncResult object was not created by calling BeginRead on this class. | |
| EndRead is called multiple times. |
EndRead must be called exactly for every call to BeginRead. Failing to end a read process before beginning another read can cause undesirable behavior such as deadlock.
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. | |
| Write to a text file. | |
| Read from a text file. | |
| Append text to a file. | |
| Rename or move a file. | |
| Copy a file. | |
| Get the size of a file. | |
| Get the attributes of a file. | |
| Set the attributes of a file. | |
| Determine if a file exists. | |
| Read from a binary file. | |
| Write to a binary file. | |
| Create a directory. | Directory.CreateDirectory |
This code example is part of a larger example provided for the FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) constructor.
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( "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(); } public:
static void EndReadCallback(IAsyncResult asyncResult)
{
State tempState = ((State)(asyncResult.get_AsyncState()));
int readCount = tempState.get_FStream().EndRead(asyncResult);
int i = 0;
while((i < readCount)) {
if ( tempState.get_ReadArray()[i] !=
tempState.get_WriteArray()[i++] ) {
Console.WriteLine("Error writing data.");
tempState.get_FStream().Close();
return;
}
}
Console.WriteLine("The data was written to {0} and verified.",
tempState.get_FStream().get_Name());
tempState.get_FStream().Close();
// Signal the main thread that the verification is finished.
tempState.get_ManualEvent().Set();
} //EndReadCallback
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.