FileStream.EndRead(IAsyncResult) Metodo

Definizione

Attende il completamento dell'operazione di lettura asincrona in sospeso.Waits for the pending asynchronous read operation to complete. (In alternativa, si consideri l'uso di ReadAsync(Byte[], Int32, Int32, CancellationToken).)(Consider using ReadAsync(Byte[], Int32, Int32, CancellationToken) instead.)

public:
 override int EndRead(IAsyncResult ^ asyncResult);
public override int EndRead (IAsyncResult asyncResult);
override this.EndRead : IAsyncResult -> int
Public Overrides Function EndRead (asyncResult As IAsyncResult) As Integer

Parametri

asyncResult
IAsyncResult

Riferimento alla richiesta asincrona in sospeso da attendere.The reference to the pending asynchronous request to wait for.

Restituisce

Int32

Numero di byte letti dal flusso, tra 0 e il numero di byte richiesto.The number of bytes read from the stream, between 0 and the number of bytes you requested. I flussi restituiscono 0 solo alla fine del flusso; in caso contrario, si devono bloccare fino a quando non è disponibile almeno 1 byte.Streams only return 0 at the end of the stream, otherwise, they should block until at least 1 byte is available.

Eccezioni

asyncResult costruito in modo predefinito è null.asyncResult is null.

L'oggetto IAsyncResult non è stato creato chiamando BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) in questa classe.This IAsyncResult object was not created by calling BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) on this class.

EndRead(IAsyncResult) viene chiamato più volte.EndRead(IAsyncResult) is called multiple times.

Il flusso è chiuso o si è verificato un errore interno.The stream is closed or an internal error has occurred.

Esempio

Questo esempio di codice fa parte di un esempio più ampio fornito per il FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) costruttore.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.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();
}
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

Commenti

In .NET Framework 4 e versioni precedenti è necessario usare metodi quali BeginRead e EndRead per implementare operazioni asincrone sui file.In the .NET Framework 4 and earlier versions, you have to use methods such as BeginRead and EndRead to implement asynchronous file operations. Questi metodi sono ancora disponibili in .NET Framework 4.5.NET Framework 4.5 per supportare il codice legacy. Tuttavia, i nuovi metodi asincroni, ad esempio ReadAsync , WriteAsync , CopyToAsync e FlushAsync , consentono di implementare più facilmente le operazioni asincrone sui file.These methods are still available in the .NET Framework 4.5.NET Framework 4.5 to support legacy code; however, the new async methods, such as ReadAsync, WriteAsync, CopyToAsync, and FlushAsync, help you implement asynchronous file operations more easily.

EndRead deve essere chiamato esattamente per ogni chiamata a BeginRead .EndRead must be called exactly for every call to BeginRead. La mancata esecuzione di un processo di lettura prima di iniziare un'altra lettura può causare comportamenti indesiderati come il deadlock.Failing to end a read process before beginning another read can cause undesirable behavior such as deadlock.

Questo metodo esegue l'override di EndRead.This method overrides EndRead.

EndRead può essere chiamato su ogni IAsyncResult da BeginRead .EndRead can be called on every IAsyncResult from BeginRead. EndReadLa chiamata di indica il numero di byte letti dal flusso.Calling EndRead tells you how many bytes were read from the stream. EndRead si bloccherà fino al completamento dell'operazione di I/O.EndRead will block until the I/O operation has completed.

Si applica a

Vedi anche