C# 处理我刚刚用ActiveMQ和C发送的消息

C# 处理我刚刚用ActiveMQ和C发送的消息,c#,activemq,windows-forms-designer,C#,Activemq,Windows Forms Designer,我是将ActiveMQ与C结合使用的初学者。我创建了一个简单的windows窗体,其中包含一个按钮和一个标签。当我单击该按钮时,我向队列发送一条消息,标签用我刚才发送的消息初始化。当然,我可以直接初始化标签,但我希望我的表单使用队列中的消息来更新标签 问题是我无法以相同的形式处理消息以更新标签。我的使用者代码根本没有被调用,但它在表单的加载事件中被初始化。 这是密码 protected override void OnLoad(EventArgs e) { bas

我是将ActiveMQ与C结合使用的初学者。我创建了一个简单的windows窗体,其中包含一个按钮和一个标签。当我单击该按钮时,我向队列发送一条消息,标签用我刚才发送的消息初始化。当然,我可以直接初始化标签,但我希望我的表单使用队列中的消息来更新标签

问题是我无法以相同的形式处理消息以更新标签。我的使用者代码根本没有被调用,但它在表单的加载事件中被初始化。 这是密码

    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);
                    }
                }
            }
        }
    }
如果我关闭windows窗体并重新启动它,则我的标签会得到很好的更新,但我不想关闭它并重新打开它


伙计们,你们有什么想法吗?

试着用这样的事件委托创建一个类

订户类

Winforms 在windows窗体中,按如下方式订阅队列

    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;
        }
    }
资源: 不幸的是,没有那么多的资源来教授C&ActiveMQ。尝试使用,因为这是相当好的


试着看一篇文章。免责声明:这是我的网站,这篇文章是我写的。对不起,这是自我宣传。但我觉得这与主题相关。

尝试创建一个具有类似事件委托的类

订户类

Winforms 在windows窗体中,按如下方式订阅队列

    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;
        }
    }
资源: 不幸的是,没有那么多的资源来教授C&ActiveMQ。尝试使用,因为这是相当好的


试着看一篇文章。免责声明:这是我的网站,这篇文章是我写的。对不起,这是自我宣传。但我觉得这与主题相关。

另一个资源:ActiveMQ在.NET示例聊天应用程序中另一个资源:ActiveMQ在.NET示例聊天应用程序中