FileStream.EndRead(IAsyncResult) Méthode

Définition

Attend que l'opération de lecture asynchrone en attente se termine. (Utilisez ReadAsync(Byte[], Int32, Int32, CancellationToken) à la place.)

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

Paramètres

asyncResult
IAsyncResult

Référence à la requête asynchrone en attente qu'il faut attendre.

Retours

Nombre d'octets lus dans le flux, entre 0 et le nombre d'octets demandés. Les flux retournent 0 seulement à la fin du flux, sinon ils doivent être bloqués jusqu'à ce qu'au moins 1 octet soit disponible.

Exceptions

asyncResult a la valeur null.

Cet objet IAsyncResult n’a pas été créé en appelant BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) sur cette classe.

EndRead(IAsyncResult) est appelé plusieurs fois.

Le flux est fermé ou une erreur interne s’est produite.

Exemples

Cet exemple de code fait partie d’un exemple plus grand fourni pour le FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) constructeur.

   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();
}
let endReadCallback (asyncResult: IAsyncResult) =
    let tempState = asyncResult.AsyncState :?> State
    let readCount = tempState.FStream.EndRead asyncResult

    let mutable i = 0
    let mutable errored = false

    while i < readCount do
        if tempState.ReadArray[i] <> tempState.WriteArray[i] then
            printfn "Error writing data."
            tempState.FStream.Close()
            errored <- true
            i <- readCount

        i <- i + 1

    printfn $"The data was written to {tempState.FStream.Name} and verified."
    tempState.FStream.Close()
    // Signal the main thread that the verification is finished.
    tempState.ManualEvent.Set() |> ignore
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

Remarques

Dans .NET Framework 4 et les versions antérieures, vous devez utiliser des méthodes telles que BeginRead et EndRead pour implémenter des opérations de fichier asynchrones. Ces méthodes sont toujours disponibles dans .NET Framework 4.5 pour prendre en charge le code hérité ; Toutefois, les nouvelles méthodes asynchrones, telles que ReadAsync, WriteAsync, CopyToAsyncet FlushAsync, vous aident à implémenter plus facilement des opérations de fichier asynchrones.

EndRead doit être appelé exactement pour chaque appel à BeginRead. Le fait de ne pas mettre fin à un processus de lecture avant d’en commencer une autre peut entraîner un comportement indésirable tel qu’un blocage.

Cette méthode se substitue à EndRead.

EndRead peut être appelé sur tous les IAsyncResult à partir de BeginRead. L’appel EndRead vous indique le nombre d’octets lus à partir du flux. EndRead se bloque jusqu’à ce que l’opération d’E/S soit terminée.

S’applique à

Voir aussi