Condividi tramite


NegotiateStream.EndRead(IAsyncResult) Metodo

Definizione

Termina un'operazione di lettura asincrona avviata con la chiamata del metodo BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).

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

Istanza della classe IAsyncResult restituita da una chiamata di BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).

Restituisce

Valore Int32 che specifica il numero di byte letti dal flusso sottostante.

Eccezioni

asyncResult è null.

asyncResult non è stato creato da una chiamata del metodo BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).

Non esiste alcuna operazione di lettura in sospeso da completare.

-oppure-

L'autenticazione non è stata effettuata.

L'operazione di lettura non è riuscita.

Esempio

L'esempio di codice seguente illustra la fine di un'operazione di lettura asincrona. Per un esempio che illustra l'avvio dell'operazione, vedere BeginRead.

private static void EndReadCallback(ClientState cState, int bytes)
{
    NegotiateStream authStream = (NegotiateStream)cState.AuthenticatedStream;
    // Read the client message.
    try
    {
        cState.Message.Append(Encoding.UTF8.GetChars(cState.Buffer, 0, bytes));
        if (bytes != 0)
        {
            Task<int> readTask = authStream.ReadAsync(cState.Buffer, 0, cState.Buffer.Length);
            readTask
                .ContinueWith(task => { EndReadCallback(cState, task.Result); })
                .Wait();

            return;
        }
    }
    catch (Exception e)
    {
        // A real application should do something
        // useful here, such as logging the failure.
        Console.WriteLine("Client message exception:");
        Console.WriteLine(e);
        return;
    }
    IIdentity id = authStream.RemoteIdentity;
    Console.WriteLine("{0} says {1}", id.Name, cState.Message.ToString());
}

Commenti

Se l'operazione non è stata completata, questo metodo blocca fino a quando non viene eseguito.

Per eseguire questa operazione in modo sincrono, usare il Read metodo .

Non è possibile chiamare questo metodo fino a quando non è stata eseguita correttamente l'autenticazione. Per eseguire l'autenticazioneAuthenticateAsClient, chiamare uno dei metodi , AuthenticateAsServerAsyncAuthenticateAsServerAuthenticateAsClientAsyncBeginAuthenticateAsCliento .BeginAuthenticateAsServer

Si applica a