Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使循环在C sharp中工作并在RabbitMQ中排队消息_C#_Loops - Fatal编程技术网

C# 使循环在C sharp中工作并在RabbitMQ中排队消息

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队列上的消息负载进行排队,以测试它,并查看是否可以中断它。我不明白为什么它不起作用:

我在循环中尝试做的是让它发布消息4次,然后完成。这段代码中可能有什么错误

代码:

使用系统;
使用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++;
    }