Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

IInputChannel::TryReceive Method (TimeSpan, Message^%)

 

Tries to receive a message within a specified interval of time.

Namespace:   System.ServiceModel.Channels
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

bool TryReceive(
	TimeSpan timeout,
	[OutAttribute] Message^% message
)

Parameters

timeout
Type: System::TimeSpan

The IAsyncResult returned by a call to one of the BeginReceive methods.

message
Type: System.ServiceModel.Channels::Message^%

The Message received.

Return Value

Type: System::Boolean

true if a message is received before the timeout has been exceeded; otherwise false.

Exception Condition
TimeoutException

The specified timeout is exceeded before the operation is completed.

ArgumentOutOfRangeException

The timeout specified is less than zero.

If you are going to handle timeouts and not just re-throw or wrap the TimeoutException, then you should call TryReceive(TimeSpan, Message^%) instead of Receive.

If you are not going to treat timeouts specially then just call Receive, otherwise you will lose error information.

The following code illustrates how to implement this method:

public bool TryReceive(TimeSpan timeout, out Message message)
{
    bool result;
    while (true)
    {
        result = this.InnerChannel.TryReceive(timeout, out message);
        if (ProcessReceivedMessage(ref message))
        {
            break;
        }
    }

    return result;
}

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show:
© 2017 Microsoft