我有这个问题。我需要能够从 OnMessage 中将传入消息附加到 txtConsole,但我收到了 Illegal Cross Thread 错误。我该如何解决这个问题?我在 C# 方面非常基础,所以一些带有解释的代码(没有伪代码)会有所帮助。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Apache.NMS;
using Apache.NMS.Util;
namespace WindowsFormsApplication1
{
public partial class frmConsole : Form
{
public frmConsole()
{
InitializeComponent();
}
private void Form1_Load(object sender, System.EventArgs e)
{ }
public void cmdConnect_Click(object sender, EventArgs e)
{
// Output to the user that the connection is being set up
txtConsole.AppendText(Environment.NewLine + "Setting up connection...");
// Define the feed URL
IConnectionFactory factory = new NMSConnectionFactory(new Uri("stomp:tcp://datafeeds.networkrail.co.uk:61618"));
// Define the credentials
IConnection connection = factory.CreateConnection("REDACTED", "REDACTED");
// Create the session
ISession session = connection.CreateSession();
// Specify which feed - we want TRAIN_MVT_ALL_TOC to listen for all train movements
IDestination destination = session.GetDestination("topic://" + "TRAIN_MVT_ALL_TOC");
// Let the end user know where we will be subscribing to
txtConsole.AppendText(Environment.NewLine + "Will attempt subscription to " + destination);
// Create a consumer for the feed
IMessageConsumer consumer = session.CreateConsumer(destination);
// Let the end user know we are about to connect...
txtConsole.AppendText(Environment.NewLine + "Connecting...");
// Connection details are now all set up. Start the connection...
connection.Start();
// Check we are connected
if (connection.IsStarted == false)
{
txtConsole.AppendText(Environment.NewLine + "Connection closed.");
connection.Close();
}
// Now we need to handle messages using a MessageListener where we pass it to the OnMessage void.
consumer.Listener += new MessageListener(OnMessage);
txtConsole.AppendText(Environment.NewLine + "Connection established. Waiting for messages...");
// End of void
}
public void OnMessage(IMessage message)
{
ITextMessage msg = (ITextMessage)message;
message.Acknowledge();
txtConsole.AppendText(Environment.NewLine + msg.Text);
}
}
}