Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dequeue from messageQueue in the PeekCompleted Method

i'm reading messages from MessageQueue using PeekCompleted, i do my process here and if everything go right, I need to remove it from the Queue! currenty i am using MyMessageQueue.Receive() and it works, but is this a reliable way of making sure each message will be processed right?

    MessageQueue MyMessageQueue;
    public Form1()
    {
        InitializeComponent();

        MyMessageQueue = new MessageQueue(@".\private$\Dms");
        MyMessageQueue.PeekCompleted += new PeekCompletedEventHandler(MessageQueue_PeekCompleted);
        MyMessageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
        MyMessageQueue.BeginPeek();
    }

    void MessageQueue_PeekCompleted(object sender, PeekCompletedEventArgs e)
    {
        try
        {
            Debug.WriteLine("ToProcess:" + e.Message.Body);
            //Long process that maybe fail
            MyMessageQueue.Receive();
        }
        finally
        {
            MyMessageQueue.BeginPeek();
        }
    }
like image 293
Fraga Avatar asked Nov 29 '25 08:11

Fraga


1 Answers

Receive() receives the first message in the queue, removing it from the queue. What you need is MessageQueue.ReceiveById Method

MyMessageQueue.ReceiveById(e.Message.Id);

Secondly, I think, you always need to call MessageQueue.EndPeek Method

Message m = MyMessageQueue.EndPeek(e.AsyncResult);
like image 88
Jacob Seleznev Avatar answered Nov 30 '25 22:11

Jacob Seleznev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!