Export (0) Print
Expand All

MessageQueue.BeginPeek Method (TimeSpan, Object, AsyncCallback)

Initiates an asynchronous peek operation that has a specified time-out and a specified state object, which provides associated information throughout the operation's lifetime. This overload receives notification, through a callback, of the identity of the event handler for the operation. The operation is not complete until either a message becomes available in the queue or the time-out occurs.

Namespace:  System.Messaging
Assembly:  System.Messaging (in System.Messaging.dll)

public IAsyncResult BeginPeek(
	TimeSpan timeout,
	Object stateObject,
	AsyncCallback callback
)

Parameters

timeout
Type: System.TimeSpan

A TimeSpan that indicates the interval of time to wait for a message to become available.

stateObject
Type: System.Object

A state object, specified by the application, that contains information associated with the asynchronous operation.

callback
Type: System.AsyncCallback

The AsyncCallback that will receive the notification of the asynchronous operation completion.

Return Value

Type: System.IAsyncResult
The IAsyncResult that identifies the posted asynchronous request.

ExceptionCondition
ArgumentException

The value specified for the timeout parameter is not valid.

MessageQueueException

An error occurred when accessing a Message Queuing method.

When you use this overload, the callback specified in the callback parameter is invoked directly when a message becomes available in the queue or when the specified interval of time has expired; the PeekCompleted event is not raised. The other overloads of BeginPeek rely on this component to raise the PeekCompleted event.

PeekCompleted is also raised if a message already exists in the queue.

The BeginPeek method returns immediately, but the asynchronous operation is not completed until the event handler is called.

Because BeginPeek is asynchronous, you can call it to peek the queue without blocking the current thread of execution. To synchronously peek the queue, use the Peek method.

Once an asynchronous operation completes, you can call BeginPeek or BeginReceive again in the event handler to keep receiving notifications.

BeginPeek returns a IAsyncResult that identifies the asynchronous operation that the method started. You can use this IAsyncResult throughout the lifetime of the operation, although you generally do not use it until EndPeek(IAsyncResult) is called. However, if you start several asynchronous operations, you can place their IAsyncResult values in an array and specify whether to wait for all operations or any operation to complete. In this case, you use the AsyncWaitHandle property of the IAsyncResult to identify the completed operation.

The state object associates state information with the operation. For example, if you call BeginPeek multiple times to initiate multiple operations, you can identify each operation through a separate state object that you define.

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode

Available

Local computer

Yes

Local computer and direct format name

Yes

Remote computer

No

Remote computer and direct format name

Yes

The following code example creates an asynchronous peek operation. The code example sends a message to a local message queue, then calls BeginPeek(TimeSpan, Object, AsyncCallback), passing in: a time-out value of ten seconds; a unique integer that identifies that particular message; and a new instance of AsyncCallback that identifies the event handler, MyPeekCompleted. When a PeekCompleted event is raised, the event handler peeks at the message and writes the message body and the integer message identifier to the screen.


using System;
using System.Messaging;

public class QueueExample
{
    // Represents a state object associated with each message. 
    static int messageNumber = 0;

    public static void Main()
    {
        // Create a non-transactional queue on the local computer. 
        // Note that the queue might not be immediately accessible, and 
        // therefore this example might throw an exception of type 
        // System.Messaging.MessageQueueException when trying to send a 
        // message to the newly created queue.
        CreateQueue(".\\exampleQueue", false);

        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\exampleQueue");

        // Send a message to the queue.
        queue.Send("Example Message");

        // Begin the asynchronous peek operation.
        queue.BeginPeek(TimeSpan.FromSeconds(10.0), messageNumber++,
            new AsyncCallback(MyPeekCompleted));

        // Simulate doing other work on the current thread.
        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10.0));

        return;
    }

    // Creates a new queue. 
    public static void CreateQueue(string queuePath, bool transactional)
    {
        if(!MessageQueue.Exists(queuePath))
        {
            MessageQueue.Create(queuePath, transactional);
        }
        else
        {
            Console.WriteLine(queuePath + " already exists.");
        }
    }

    // Provides an event handler for the PeekCompleted event. 
    private static void MyPeekCompleted(IAsyncResult asyncResult)
    {
        // Connect to the queue.
        MessageQueue queue = new MessageQueue(".\\exampleQueue");

        // End the asynchronous peek operation.
        Message msg = queue.EndPeek(asyncResult);

        // Display the message information on the screen.
        Console.WriteLine("Message number: {0}", (int)asyncResult.AsyncState);
        Console.WriteLine("Message body: {0}", (string)msg.Body);

        // Receive the message. This will remove the message from the queue.
        msg = queue.Receive(TimeSpan.FromSeconds(10.0));
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft