Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6943741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:11:45+00:00 2026-05-27T13:11:45+00:00

I’m a beginner at using ActiveMQ with C#. I’ve created a simple windows form

  • 0

I’m a beginner at using ActiveMQ with C#. I’ve created a simple windows form with one button and one label. When I click on the button, i send a message to the queue and the label is initialized with the message I just sent. Of course, I could initialize my label directly but I want my form to rather consume the message from the queue in order to update my label.

The problem is I don’t manage to handle the message in the same form to update my label. My consumer code is not called at all and yet, its initialized in the Load event of my form.
Here’s the code

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        InitializeHandlerAMQ();
    }

    private void InitializeHandlerAMQ()
    {
        Tchat tchat = null;
        IDestination dest = _session.GetQueue(QUEUE_DESTINATION);
        using(IMessageConsumer consumer = _session.CreateConsumer(dest))
        {
            IMessage message;
            while((message = consumer.Receive(TimeSpan.FromMilliseconds(2000))) != null)
            {
                var objectMessage = message as IObjectMessage;
                if(objectMessage != null)
                {
                    tchat = objectMessage.Body as Tchat;
                    if (tchat != null)
                    {
                        textBox2.Text += string.Format("{0}{1}", tchat.Message, Environment.NewLine);
                    }
                }
            }
        }
    }

If I close my windows form and restart it, then my label is well updated but I don’t want to close it and re open it.

Do you have any ideas guys ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:11:46+00:00Added an answer on May 27, 2026 at 1:11 pm

    Try creating a class with an event delegate like this.

    A subscriber class

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Apache.NMS;
    using Apache.NMS.ActiveMQ;
    using Apache.NMS.ActiveMQ.Commands;
    
    namespace Utilities
    {
        public delegate void QMessageReceivedDelegate(string message);
        public class MyQueueSubscriber : IDisposable
        {
            private readonly string topicName = null;
            private readonly IConnectionFactory connectionFactory;
            private readonly IConnection connection;
            private readonly ISession session;
            private readonly IMessageConsumer consumer;
            private bool isDisposed = false;
            public event QMessageReceivedDelegate OnMessageReceived;
    
            public MyQueueSubscriber(string queueName, string brokerUri, string clientId)
            {
                this.topicName = queueName;
                this.connectionFactory = new ConnectionFactory(brokerUri);
                this.connection = this.connectionFactory.CreateConnection();
                this.connection.ClientId = clientId;
                this.connection.Start();
                this.session = connection.CreateSession();
                ActiveMQQueue topic = new ActiveMQQueue(queueName);
                //this.consumer = this.session.CreateDurableConsumer(topic, consumerId, "2 > 1", false);
                this.consumer = this.session.CreateConsumer(topic, "2 > 1");
                this.consumer.Listener += new MessageListener(OnMessage);
    
            }
    
            public void OnMessage(IMessage message)
            {
                ITextMessage textMessage = message as ITextMessage;
                if (this.OnMessageReceived != null)
                {
                    this.OnMessageReceived(textMessage.Text);
                }
            }
    
            #region IDisposable Members
    
            public void Dispose()
            {
                if (!this.isDisposed)
                {
                    this.consumer.Dispose();
                    this.session.Dispose();
                    this.connection.Dispose();
                    this.isDisposed = true;
                }
            }
    
            #endregion
    
        }
    }
    

    Winforms
    In your windows form Subscribe to the queue like this

        MyQueueSubscriber QueueSubscriber = new MyQueueSubscriber(QueueName, ActiveMQHost, QueueClientId);
        QueueSubscriber.OnMessageReceived += new QMessageReceivedDelegate(QueueSubscriber_OnMessageReceived);
    
    static void QueueSubscriber_OnMessageReceived(string message)
    {
            SetText(message);
    }
    
        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.textBox1.InvokeRequired)
            {   
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.labelname.value = text;
            }
        }
    

    Resources:
    Unfortunately there are not that many resources to teach C# & ActiveMQ. Try using http://activemq.apache.org/nms/ as this was quite good.

    Try looking at a small article from http://www.codersource.net/MicrosoftNet/CAdvanced/PublishSubscribeinCusingActiveMQ.aspx. Disclaimer: This is my website and the article was written by me. Sorry for the self publicity. But I feel this is relevant to the topic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.