C# 使循环在C sharp中工作并在RabbitMQ中排队消息
我有下面的代码,我正在尝试对rabbitmq队列上的消息负载进行排队,以测试它,并查看是否可以中断它。我不明白为什么它不起作用: 我在循环中尝试做的是让它发布消息4次,然后完成。这段代码中可能有什么错误 代码:C# 使循环在C sharp中工作并在RabbitMQ中排队消息,c#,loops,C#,Loops,我有下面的代码,我正在尝试对rabbitmq队列上的消息负载进行排队,以测试它,并查看是否可以中断它。我不明白为什么它不起作用: 我在循环中尝试做的是让它发布消息4次,然后完成。这段代码中可能有什么错误 代码: 使用系统; 使用RabbitMQ.Client; 使用系统文本; 类发送 { 公共静态void Main() { var factory=new ConnectionFactory(){HostName=“localhost”}; 使用(var connection=factory.Cr
使用系统;
使用RabbitMQ.Client;
使用系统文本;
类发送
{
公共静态void Main()
{
var factory=new ConnectionFactory(){HostName=“localhost”};
使用(var connection=factory.CreateConnection())
{
使用(var channel=connection.CreateModel())
{
//对于(int-myInt=0;myInt<699;)
int-myInt=1;
做
虽然(myInt您将return语句放在while循环中,这会导致程序在循环一次后终止。此外,您忘了增加变量myInt
,因此即使移动return语句,也会遇到无限循环的问题。此外,您不需要do
keyword、 您的while循环应该如下所示:
while (myInt <= 4)
{
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish("", "test", null, body);
Console.WriteLine(" [x] Sent {0}", message);
Console.Read();
myInt++;
}
while(myInt)我不明白“它不工作”的意思。发布一个或至少是实际与预期的结果。你从Main()
第一次循环运行时返回没有问题。干杯!
using System;
using RabbitMQ.Client;
using System.Text;
class Send
{
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
{ for (int myInt = 100; myInt <= 100000 ; myInt++)
using (var channel = connection.CreateModel())
while (myInt <= 100000)
{
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish("", "hello", null, body);
Console.WriteLine(" [x] Sent {0}", message);
Console.Read();
myInt++;
}
//myInt++;
}
//myInt++;
//return;
// myInt++;
}
}
while (myInt <= 4)
{
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish("", "test", null, body);
Console.WriteLine(" [x] Sent {0}", message);
Console.Read();
myInt++;
}