我有一个简单的 C# 应用程序,它每秒向我的 RabbitMQ 交换发送一条消息。当我的互联网连接断开时,应用程序会崩溃。所以我添加了一个 Try/Catch 语句,现在它不再崩溃了。但是,当连接恢复时,它将不再发送数据。我必须关闭应用程序,然后重新打开它。我这样做正确吗?
private void rabbitmqxmit()
{
try
{
while (rmqtxrun == true)
{
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = textBox3.Text;
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel())
{
button1.BackColor = Color.Green;
string message = textBox1.Text;
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
channel.BasicPublish(textboxExchange.Text, textboxKey.Text, null, body);
txtboxTransmitting.Text = message;
button1.BackColor = Color.Gray;
Thread.Sleep(Convert.ToInt32(textBox4.Text));
}
}
}
catch {}
}