Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

MessageQueue.Peek Method

Returns a copy of the first message in the queue, without removing the message from the queue.

Overload List

Returns without removing (peeks) the first message in the queue referenced by this MessageQueue. The Peek method is synchronous, so it blocks the current thread until a message becomes available.

[Visual Basic] Overloads Public Function Peek() As Message
[C#] public Message Peek();
[C++] public: Message* Peek();
[JScript] public function Peek() : Message;

Returns without removing (peeks) the first message in the queue referenced by this MessageQueue. The Peek method is synchronous, so it blocks the current thread until a message becomes available or the specified time-out occurs.

[Visual Basic] Overloads Public Function Peek(TimeSpan) As Message
[C#] public Message Peek(TimeSpan);
[C++] public: Message* Peek(TimeSpan);
[JScript] public function Peek(TimeSpan) : Message;

Example

[Visual Basic, C#, C++] The following example uses the Peek method with a time-out of zero to check whether the queue is empty.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Peek. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Messaging

Namespace MyProject

    '/ <summary>
    '/ Provides a container class for the example.
    '/ </summary>
    Public Class MyNewQueue


        '**************************************************
        ' Provides an entry point into the application.
        '         
        ' This example determines whether a queue is empty.
        '**************************************************

        Public Shared Sub Main()

            ' Create a new instance of the class.
            Dim myNewQueue As New MyNewQueue()

            ' Determine whether a queue is empty.
            Dim isQueueEmpty As Boolean = myNewQueue.IsQueueEmpty()

            Return

        End Sub 'Main


        '**************************************************
        ' Determines whether a queue is empty. The Peek()
        ' method throws an exception if there is no message
        ' in the queue. This method handles that exception 
        ' by returning true to the calling method.
        '**************************************************

        Public Function IsQueueEmpty() As Boolean

            Dim queueEmpty As Boolean = False

            ' Connect to a queue.
            Dim myQueue As New MessageQueue(".\myQueue")

            Try

                ' Set Peek to return immediately.
                myQueue.Peek(New TimeSpan(0))

                ' If an IOTimeout was not thrown, there is a message 
                ' in the queue.
                queueEmpty = False

            Catch e As MessageQueueException

                If e.MessageQueueErrorCode = _
                    MessageQueueErrorCode.IOTimeout Then

                    ' No message was in the queue.
                    IsQueueEmpty = True

                End If

                ' Handle other sources of MessageQueueException as necessary.

                ' Handle other exceptions as necessary.

            End Try

            ' Return true if there are no messages in the queue.
            Return queueEmpty

        End Function 'IsQueueEmpty 

    End Class 'MyNewQueue
End Namespace 'MyProject

[C#] 
using System;
using System.Messaging;

namespace MyProject
{
    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        //**************************************************
        // Provides an entry point into the application.
        //         
        // This example determines whether a queue is empty.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Determine whether a queue is empty.
            bool isQueueEmpty = myNewQueue.IsQueueEmpty();
                        
            return;
        }


        //**************************************************
        // Determines whether a queue is empty. The Peek()
        // method throws an exception if there is no message
        // in the queue. This method handles that exception 
        // by returning true to the calling method.
        //**************************************************
        
        public bool IsQueueEmpty()
        {
            bool isQueueEmpty = false;

            // Connect to a queue.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");

            try
            {
                // Set Peek to return immediately.
                myQueue.Peek(new TimeSpan(0));

                // If an IOTimeout was not thrown, there is a message 
                // in the queue.
                isQueueEmpty = false;
            }

            catch(MessageQueueException e)
            {
                if (e.MessageQueueErrorCode == 
                    MessageQueueErrorCode.IOTimeout)
                {
                    // No message was in the queue.
                    isQueueEmpty = true;
                }

                // Handle other sources of MessageQueueException.
            }

            // Handle other exceptions as necessary.

            // Return true if there are no messages in the queue.
            return isQueueEmpty;

        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;

__gc class MyNewQueue
{
    //*************************************************
    // Determines whether a queue is empty. The Peek()
    // method throws an exception if there is no message
    // in the queue. This method handles that exception 
    // by returning true to the calling method.
    //*************************************************
public:
    bool IsQueueEmpty() 
    {
        bool isQueueEmpty = false;

        // Connect to a queue.
        MessageQueue* myQueue = new MessageQueue(S".\\myQueue");

        try 
        {
            // Set Peek to return immediately.
            myQueue->Peek(TimeSpan(0));

            // If an IOTime->Item[Out] was* not thrown, there is a message 
            // in the queue.
            isQueueEmpty = false;
        }
        catch (MessageQueueException* e) 
        {
            if (e->MessageQueueErrorCode == 
                MessageQueueErrorCode::IOTimeout) 
            {
                // No message was in the queue.
                isQueueEmpty = true;
            }

            // Handle other sources of MessageQueueException.
        }

        // Handle other exceptions as necessary.

        // Return true if there are no messages in the queue.
        return isQueueEmpty;
    }
};

//*************************************************
// Provides an entry point into the application.
//         
// This example determines whether a queue is empty.
//*************************************************

int main() 
{
    // Create a new instance of the class.
    MyNewQueue* myNewQueue = new MyNewQueue();

    // Determine whether a queue is empty.
    bool isQueueEmpty = myNewQueue->IsQueueEmpty();

    return 0;
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

MessageQueue Class | MessageQueue Members | System.Messaging Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.