FileStream.EndRead(IAsyncResult) Metodo

Definizione

Attende il completamento dell'operazione di lettura asincrona in sospeso. Prendere invece in considerazione l'uso ReadAsync(Byte[], Int32, Int32, CancellationToken) di .

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.

Valori restituiti

Numero di byte letti dal flusso, compreso tra 0 e il numero di byte richiesti. I flussi restituiscono solo 0 alla fine del flusso. In caso contrario, devono bloccarsi fino a quando non sono disponibili almeno 1 byte.

Eccezioni

asyncResult è null.

Questo IAsyncResult oggetto non è stato creato chiamando BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) su 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 = (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

Commenti

In .NET Framework 4 e versioni precedenti è necessario usare metodi come BeginRead e EndRead per implementare operazioni di file asincrone. Questi metodi sono ancora disponibili in .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 di file asincrone.

EndRead deve essere chiamato esattamente per ogni chiamata a BeginRead. L'esito negativo di un processo di lettura prima di iniziare un'altra lettura può causare comportamenti indesiderati, ad esempio deadlock.

Questo metodo esegue l'override di EndRead.

EndRead può essere chiamato su ogni IAsyncResult da BeginRead. La chiamata EndRead indica quanti byte sono stati letti dal flusso. EndRead bloccherà fino al completamento dell'operazione di I/O.

Si applica a

Vedi anche