IInputChannel.BeginTryReceive(TimeSpan, AsyncCallback, Object) 方法

定义

开始用于接收消息的异步操作,该操作具有指定的超时值和状态对象与之关联。Begins an asynchronous operation to receive a message that has a specified time out and state object associated with it.

public:
 IAsyncResult ^ BeginTryReceive(TimeSpan timeout, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginTryReceive (TimeSpan timeout, AsyncCallback callback, object state);
abstract member BeginTryReceive : TimeSpan * AsyncCallback * obj -> IAsyncResult
Public Function BeginTryReceive (timeout As TimeSpan, callback As AsyncCallback, state As Object) As IAsyncResult

参数

timeout
TimeSpan

一个 TimeSpan,指定等待消息变为可用所用的时间间隔。The TimeSpan that specifies the interval of time to wait for a message to become available.

callback
AsyncCallback

接收异步操作完成通知的 AsyncCallback 委托。The AsyncCallback delegate that receives the notification of the asynchronous operation completion.

state
Object

一个由应用程序指定的对象,包含与异步操作相关联的状态信息。An object, specified by the application, that contains state information associated with the asynchronous operation.

返回

IAsyncResult

引用异步接收操作的 IAsyncResultThe IAsyncResult that references the asynchronous receive operation.

例外

在操作完成前超出指定的 timeoutThe specified timeout is exceeded before the operation is completed.

指定的超时小于零。The timeout specified is less than zero.

示例

下面的代码演示如何实现此方法:The following code illustrates how to implement this method:

public IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback, object state)
{
    TryReceiveAsyncResult<TChannel> result = new TryReceiveAsyncResult<TChannel>(this, timeout, callback, state);
    result.Begin();
    return result;
}

注解

如果需要应用程序处理继续进行而不等待,则使用异步 BeginTryReceive(TimeSpan, AsyncCallback, Object) 方法。Use the asynchronous BeginTryReceive(TimeSpan, AsyncCallback, Object) method when you want the application processing to continue without waiting. 如果当前线程在回复请求消息时可以被阻塞,或者一直被阻塞到超出超时间隔,则使用同步 TryReceive(TimeSpan, Message) 方法。Use the synchronous TryReceive(TimeSpan, Message) method when it is acceptable for the current thread to be blocked while it replies to the request message or until the timeout interval is exceeded.

直到通道中出现消息时或发生超时时才完成操作。The operation is not complete until either a message becomes available in the channel or the timeout occurs.

如果您打算处理超时而不仅仅是重新引发或包装 TimeoutException,则应该调用 BeginTryReceive(TimeSpan, AsyncCallback, Object) 而不是 BeginReceiveIf you are going to handle timeouts and not just re-throw or wrap the TimeoutException, then you should call BeginTryReceive(TimeSpan, AsyncCallback, Object) instead of BeginReceive.

如果您不打算专门处理超时,则仅调用 BeginReceive,否则将丢失错误信息。If you are not going to treat timeouts specially then just call BeginReceive, otherwise you lose error information.

实施者说明

如果超出指定的 false,则操作从 EndTryReceive(IAsyncResult, Message) 返回 timeoutThe operation returns false from EndTryReceive(IAsyncResult, Message) if the specified timeout is exceeded.

适用于