المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Windows Message Queue Asynchronous Peek Problem



C# Programming
09-17-2009, 05:22 AM
Hi,
I am currently Implementing a Tcp Server and a serial server which are running as services and receives events from a GUI based service controller through a message queue.
I wish to peek messages on the queue as the queue is shared by both the servers and i want the data to be received somewhere else.
I used the code provided at msdn for the asynchronous peek.
using System;
using System.Messaging;

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

/***************************************************/
// Provides an entry point into the application.
//
// This example performs asynchronous peek operation
// processing.
/***************************************************/

public static void Main()
{
// Create an instance of MessageQueue. Set its formatter.
MessageQueue myQueue = new MessageQueue(".\\myQueue");
myQueue.Formatter = new XmlMessageFormatter(new Type[]
{typeof(String)});

// Add an event handler for the PeekCompleted event.
myQueue.PeekCompleted += new
PeekCompletedEventHandler(MyPeekCompleted);

// Begin the asynchronous peek operation.
myQueue.BeginPeek();

// Do other work on the current thread.

return;
}


/***************************************************/
// Provides an event handler for the PeekCompleted
// event.
/***************************************************/

private static void MyPeekCompleted(Object source,
PeekCompletedEventArgs asyncResult)
{
// Connect to the queue.
MessageQueue mq = (MessageQueue)source;

// End the asynchronous peek operation.
Message m = mq.EndPeek(asyncResult.AsyncResult);

// Display message information on the screen.
Console.WriteLine("Message: " + (string)m.Body);

// Restart the asynchronous peek operation.
mq.BeginPeek();

return;
}
}
}
The problem is once a message is sent to the queue the peek event on service end fires perfectly, but keeps on firing for the same message. what i want is that the peek method should only fire once for any message received and then fires on consecutive message received, just like as a receive event works.
The problem lies somewhere in restarting the asynchronous peek and the logic involved in getting the messages.
Help Please!!!