Beispiel #1
0
        public static async Task RunParallelPassiveConsumerAsync(
            IConnection conn,
            MyWorkerContext ctx,
            int agentCount
            )
        {
            var agentList = new List <ConsumingAgent>();

            try
            {
                Console.WriteLine($"Sub ({agentCount}) start {ctx.Subject}");
                ctx.CronoStart();

                var listeners = new Task[agentCount];
                for (int i = 0; i < agentCount; i++)
                {
                    var agent = ConsumingAgent.CreatePassive(conn, ctx, null);
                    agentList.Add(agent);
                    listeners[i] = Task.Run(agent.PassiveWorkerAsync);
                }
                await Task.WhenAll(listeners);

                ctx.CronoStop();
                Console.WriteLine($"Sub ({agentCount}) end {ctx.Subject}");
            }
            finally
            {
                foreach (var agent in agentList)
                {
                    agent.Dispose();
                }
            }
        }
Beispiel #2
0
        public static void RunParallelPassiveConsumer(
            IConnection conn,
            MyWorkerContext ctx,
            int agentCount
            )
        {
            var agentList = new List <ConsumingAgent>();

            try
            {
                Console.WriteLine($"Sub ({agentCount}) start {ctx.Subject}");
                ctx.CronoStart();

                var cde = new CountdownEvent(agentCount);
                for (int i = 0; i < agentCount; i++)
                {
                    var agent = ConsumingAgent.CreatePassive(conn, ctx, cde);
                    agentList.Add(agent);
                    ThreadPool.QueueUserWorkItem(agent.PassiveWorker);
                }
                cde.Wait();

                ctx.CronoStop();
                Console.WriteLine($"Sub ({agentCount}) end {ctx.Subject}");
            }
            finally
            {
                foreach (var agent in agentList)
                {
                    agent.Dispose();
                }
            }
        }