语言

NegotiateStream.EndRead(IAsyncResult) 方法

定义

结束以调用调用 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

参数

返回

一个 Int32 值,该值指定从基础流中读取的字节数。

例外

asyncResult 是 null。

没有要完成的挂起读取操作。

-或-

身份验证未发生。

读取操作失败。

示例

下面的代码示例演示如何结束异步读取操作。 有关演示如何启动操作的示例,请参阅 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());
}

注解

如果操作尚未完成,此方法将一直阻止,直到它完成。

若要同步执行此操作,请使用 Read 该方法。

在成功进行身份验证之前,无法调用此方法。 若要进行身份验证,请调用其中一种AuthenticateAsClient、AuthenticateAsClientAsync、、BeginAuthenticateAsClientAuthenticateAsServer、AuthenticateAsServerAsync或BeginAuthenticateAsServer方法。

适用于