0 out of 1 rated this helpful - Rate this topic

MessageQueue Constructor

.NET Framework 1.1

Initializes a new instance of the MessageQueue class.

Overload List

Initializes a new instance of the MessageQueue class. After the default constructor initializes the new instance, you must set the instance's Path property before you can use the instance.

[Visual Basic] Public Sub New()
[C#] public MessageQueue();
[C++] public: MessageQueue();
[JScript] public function MessageQueue();

Initializes a new instance of the MessageQueue class that references the Message Queuing queue at the specified path.

[Visual Basic] Public Sub New(String)
[C#] public MessageQueue(string);
[C++] public: MessageQueue(String*);
[JScript] public function MessageQueue(String);

Initializes a new instance of the MessageQueue class that references the Message Queuing queue at the specified path and with the specified read-access restriction.

[Visual Basic] Public Sub New(String, Boolean)
[C#] public MessageQueue(string, bool);
[C++] public: MessageQueue(String*, bool);
[JScript] public function MessageQueue(String, Boolean);

Example

[Visual Basic, C#, C++] The following example creates a new MessageQueue with exclusive access, sets its path, and sends a message to the queue.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the MessageQueue constructor. 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 connects to a message queue, and
        ' requests exclusive read access to the queue.
        '**************************************************

        Public Shared Sub Main()

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

            ' Output the count of Lowest priority messages.
            myNewQueue.GetExclusiveAccess()

            Return

        End Sub 'Main


        '**************************************************
        ' Requests exlusive read access to the queue. If
        ' access is granted, receives a message from the 
        ' queue.
        '**************************************************

        Public Sub GetExclusiveAccess()

            Try

                ' Request exclusive read access to the queue.
                Dim myQueue As New MessageQueue(".\myQueue", True)

                ' Receive a message. This is where a SharingViolation 
                ' exception would be thrown.
                Dim myMessage As Message = myQueue.Receive()

            Catch e As MessageQueueException

                ' Handle request for denial of exclusive read access.
                If e.MessageQueueErrorCode = _
                    MessageQueueErrorCode.SharingViolation Then

                    Console.WriteLine("Denied exclusive read access.")

                End If

                ' Handle other sources of a MessageQueueException.

                ' Handle other exceptions as necessary.

            End Try

            Return

        End Sub 'GetExclusiveAccess

    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 connects to a message queue, and
        // requests exclusive read access to the queue.
        //**************************************************

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

            // Output the count of Lowest priority messages.
            myNewQueue.GetExclusiveAccess();
                        
            return;
        }


        //**************************************************
        // Requests exlusive read access to the queue. If
        // access is granted, receives a message from the 
        // queue.
        //**************************************************
        
        public void GetExclusiveAccess()
        {
            try
            {
                // Request exclusive read access to the queue.
                MessageQueue myQueue = new 
                    MessageQueue(".\\myQueue", true);

                // Receive a message. This is where SharingViolation 
                // exceptions would be thrown.
                Message myMessage = myQueue.Receive();
            }
            
            catch (MessageQueueException e)
            {
                // Handle request for denial of exclusive read access.
                if (e.MessageQueueErrorCode == 
                    MessageQueueErrorCode.SharingViolation)
                {
                    Console.WriteLine("Denied exclusive read access");
                }

                // Handle other sources of a MessageQueueException.
            }

            // Handle other exceptions as necessary.

            return;
        }
    }
}

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

using namespace System;
using namespace System::Messaging;

__gc class MyNewQueue 
{
    // Requests exlusive read access to the queue. If
    // access is granted, receives a message from the 
    // queue.
public:
    void GetExclusiveAccess() 
    {
        try 
        {
            // Request exclusive read access to the queue.
            MessageQueue* myQueue = new MessageQueue(S".\\myQueue", true);

            // Receive a message. This is where SharingViolation 
            // exceptions would be thrown.
            Message* myMessage = myQueue->Receive();
        }
        catch (MessageQueueException* e) 
        {
            // Handle request for denial of exclusive read access.
            if (e->MessageQueueErrorCode == 
                MessageQueueErrorCode::SharingViolation) 
            {
                Console::WriteLine(S"Denied exclusive read access");
            }
            // Handle other sources of a MessageQueueException.
        }

        // Handle other exceptions as necessary.

        return;
    }
};

// Provides an entry point into the application.
// This example connects to a message queue, and
// requests exclusive read access to the queue.
int main() 
{
    // Create a new instance of the class.
    MyNewQueue* myNewQueue = new MyNewQueue();

    // Output the count of Lowest priority messages.
    myNewQueue->GetExclusiveAccess();

    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)
© 2013 Microsoft. All rights reserved.