Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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# 使用线程RabbitMQ使用者处理事件_C#_Multithreading_Events_Rabbitmq - Fatal编程技术网

C# 使用线程RabbitMQ使用者处理事件

C# 使用线程RabbitMQ使用者处理事件,c#,multithreading,events,rabbitmq,C#,Multithreading,Events,Rabbitmq,我在C#中使用RabbitMQ点网库,在不同的队列中有两个消费者 我想要的是: 由于某些业务逻辑,我不得不在一个消费者中等待一段时间 为此,我使用了Thread.Sleep() 问题 如果在一个事件中使用Thread.Sleep,则第二个线程也不会暂停 我的代码: consumer.Received += (model, ea) => { try { DRModel drModel = JsonConvert.DeserializeObject<DRM

我在C#中使用RabbitMQ点网库,在不同的队列中有两个消费者

我想要的是:

由于某些业务逻辑,我不得不在一个消费者中等待一段时间 为此,我使用了
Thread.Sleep()

问题

如果在一个事件中使用
Thread.Sleep
,则第二个线程也不会暂停

我的代码:

consumer.Received += (model, ea) =>
{
    try
    {
        DRModel drModel = JsonConvert.DeserializeObject<DRModel>(Encoding.UTF8.GetString(ea.Body));
        RMQReturnType type = ProcessSubmitSMS(drModel);
        if (type == RMQReturnType.ACK)
            channel.BasicAck(ea.DeliveryTag, false);
        else
        { 
            channel.BasicNack(ea.DeliveryTag, false, true);
            Thread.Sleep(300000); // <=== SLEEP
        }
    }
    catch (Exception ex)
    {
        channel.BasicNack(ea.DeliveryTag, false, true);
        WriteLog(ControlChoice.ListError, "Exception: " + ex.Message + " | Stack Trace: " + ex.StackTrace.ToString() + " | [Consumer Event]");
    }
};


consumer.Received+=(型号,ea)=>
{
尝试
{
DRModel DRModel=JsonConvert.DeserializeObject(Encoding.UTF8.GetString(ea.Body));
RMQReturnType类型=ProcessSubmitSMS(drModel);
if(type==RMQReturnType.ACK)
channel.BasicAck(ea.DeliveryTag,false);
其他的
{ 
channel.BasicNack(ea.DeliveryTag,false,true);

Thread.Sleep(300000);//对于类来说,这似乎是一个很好的例子,您需要的是多线程中的条件睡眠。不知道您需要的确切逻辑,但类似于下面的代码:

public class Consumer
{
    public event EventHandler Received;

    public virtual void OnReceived()
    {
        Received?.Invoke(this, EventArgs.Empty);
    }
}

class Program
{
    static void Main(string[] args)
    {
        var mutex = new Mutex();

        var consumer =  new Consumer();

        consumer.Received += (model, ea) =>
        {
            try
            {
                mutex.WaitOne();
                var id = Guid.NewGuid().ToString();
                Console.WriteLine($"Start mutex {id}");
                Console.WriteLine($"Mutex finished {id}");
                Console.WriteLine($"Start sleep {id}");
                if ( new Random().Next(10000)  % 2 == 0) // randomly sleep, that your condition
                {
                    Thread.Sleep(3000); // <=== SLEEP
                }
                Console.WriteLine($"Sleep finished {id}");
            }
            catch (Exception ex)
            {
                mutex.ReleaseMutex(); // this is where you release, if something goes wrong
            }
            finally
            {
                mutex.ReleaseMutex();// always release it
            }
        };


        Parallel.For(0, 10, t =>   //running 10 threads in parallel and stops all if the condition is true
        {
            consumer.OnReceived();
        });

        Console.ReadLine();
    }

}
公共类消费者
{
收到公共事件事件处理程序;
已接收的公共虚拟无效()
{
已接收?.Invoke(此为EventArgs.Empty);
}
}
班级计划
{
静态void Main(字符串[]参数)
{
var mutex=新的mutex();
var consumer=新消费者();
消费者.已接收+=(型号,ea)=>
{
尝试
{
mutex.WaitOne();
var id=Guid.NewGuid().ToString();
WriteLine($“启动互斥锁{id}”);
WriteLine($“互斥完成{id}”);
WriteLine($“开始睡眠{id}”);
如果(new Random().Next(10000)%2==0)//随机睡眠,那么您的条件
{
Thread.Sleep(3000);///并行运行10个线程,如果条件为true,则停止所有线程
{
consumer.OnReceived();
});
Console.ReadLine();
}
}

}据我所知,我的代码中有一些逻辑错误。 在拉比特 我在两个不同的频道上创建了两个消费者活动,因此我认为它不会在这里共享我错了一个连接是共享的b/w频道,因此我明确定义了两个连接。 据我所知
消费程序块通道和通道块连接以及连接在这两个事件中都是相同的。

这样注册回调并返回
task
,而不是使用
Thread.sleep()
,但为什么需要执行
sleep
?根据我的理解Thread.sleep()执行一些业务逻辑sleep current线程,在我的情况下,它应该是sleep current event,为什么它会睡眠其他事件。互斥将工作,您只需要在代码顶部启动它,请检查我编辑的代码示例。如果条件为真,它将停止