FileStream.EndRead(IAsyncResult) Metodo

Definizione

Attende il completamento dell'operazione di lettura asincrona in sospeso. Si consiglia di usare ReadAsync(Byte[], Int32, Int32, CancellationToken).

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.

Restituisce

Int32

Numero di byte letti dal flusso, tra 0 e il numero di byte richiesto. I flussi restituiscono 0 solo alla fine del flusso; in caso contrario, si devono bloccare fino a quando non è disponibile almeno 1 byte.

Eccezioni

asyncResult è null.

L'oggetto IAsyncResult non è stato creato chiamando BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) in questa classe.

Il flusso è chiuso o si è verificato un errore interno.

Esempio

Questo esempio di codice fa parte di un esempio più ampio fornito per il FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) costruttore .

   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

Nella versione .NET Framework 4 e versioni precedenti è necessario usare metodi come e per implementare BeginRead EndRead operazioni asincrone sui file. Questi metodi sono ancora disponibili in .NET Framework 4.5 per supportare il codice legacy. Tuttavia, i nuovi metodi asincroni, ad esempio , , e , consentono di implementare più facilmente le operazioni asincrone ReadAsync WriteAsync sui CopyToAsync FlushAsync file.

EndRead deve essere chiamato esattamente per ogni chiamata a BeginRead . La mancata chiusura di un processo di lettura prima di iniziare un'altra lettura può causare un comportamento indesiderato, ad esempio un deadlock.

Questo metodo esegue l'override di EndRead.

EndRead può essere chiamato su ogni IAsyncResult da BeginRead . La EndRead chiamata di indica il numero di byte letti dal flusso. EndRead verrà bloccata fino al completamento dell'operazione di I/O.

Si applica a

Vedi anche