Click here to Skip to main content
11,639,765 members (59,598 online)
Click here to Skip to main content

Using a Message Queue Asynchronously in .NET Framework 2.0

, 25 Feb 2008 CPOL 20.8K 10
Rate this:
Please Sign up or sign in to vote.
Provides access to a queue on a Message Queuing server. We will see how to access it asynchronously.

Introduction

My first article on Message Queues: "Using Message Queue in .NET Framework 2.0 " would help you to send and receive messages from a Message Queue server of the Windows Operating System. Now there is a thing here you might not know when there are messages in the Message Queue server.

You can't keep calling a simple receive command or peek command to check whether there are any messages available in the queue. If you do so, you will find that the application hangs until it finds any messages in the queue. So to solve that problem, we can use this message queue asynchronously. For that, we need to change the approach of receiving messages.

We will use the BeginPeek to start peeking the message queue for any new message arrival, and BeginRecieve to start receiving any new messages from the queue. This method works on other threads, so it means the work of checking and receiving messages is done in the background.

When we use:

  • the BeginPeek method, an event fires up whe ever a message is found that is
    • PeekCompleted, which occurs when a message is read without being removed from the queue. This is a result of an asynchronous operation.
  • BeginRecieve method a event fires up when ever a message is recieved that is
    • ReceiveCompleted, which occurs when a message has been removed from the queue. This event is raised by an asynchronous operation.

Using the Code

BeginPeek Method
''create an instance of the messagequeue class as i did in my ealier ariticle
''call the beginpeek method
''this method can be called anytime at formload or on a button event
myQueue.BeginPeek()

Once the BeginPeek method is called, a new thread is started for checking for any messages in the queue. Now we need to handle the PeekCompleted event

'' a sub to handle the event
Private Sub CheckQueueu(ByVal sender As System.Object, _
    ByVal e As System.Messaging.PeekCompletedEventArgs) _
    Handles myQueue.PeekCompleted
    If e.Message.Label = "Example" Then
        '' Store the message in the queue for processing
        '' i am storing in a Queue
        msgQueue.Enqueue(e.Message)
        '' remove the message from the queue if its matches
        myQueue.Receive()
        '' refresh the Queue 
        myQueue.Refresh
        '' Begin a new Peek method
        '' this is to restart the BeginPeek asynchnorously
        myQueue.BeginPeek()
    End If
End Sub
BeginReceive Method
''create an instance of the messagequeue class as i did in my ealier ariticle
''call the beginpeek method
''this method can be called anytime at formload or on a button event
myQueue.BeginReceive()

Once the BeginReceive method is called, a new thread is started for checking for any messages in the queue. Now we need to handle the ReceiveCompleted event.

'' a sub to handle the event
Private Sub RecQueueu(ByVal sender As System.Object, _
    ByVal e As System.Messaging.ReceiveCompletedEventArgs) _
    Handles myQueue.ReceiveCompleted
    '' Store the message in the queue for processing
    '' i am storing in a Queue
    msgQueue.Enqueue(e.Message)
    '' remove the message from the queue if its matches
    myQueue.Receive()
    '' refresh the Queue 
    myQueue.Refresh
    '' Begin a new Revceive method
    '' this is to restart the BeginReceive asynchnorously
    myQueue.BeginReceive()
    End If
End Sub

The code above came through research and a combination of articles and examples. In my next article, I will try to explain more on both the BeginPeek and BeginReceive methods, also passing some parameters to them.

Hoping this might help you in some way.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Imran A Momin
Software Developer (Junior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 25 Feb 2008
Article Copyright 2008 by Imran A Momin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid