Example #1
0
        /// <summary>
        /// Run a job multiple times in parallel
        /// </summary>
        public static void RunManyJobs(Queue <Func <CantonJob> > jobs, int instances)
        {
            ConcurrentBag <CantonJob> currentJobs = new ConcurrentBag <CantonJob>();
            bool run = true;

            ConsoleCancelEventHandler handler = (sender, e) =>
            {
                if (run)
                {
                    Console.WriteLine("Ctrl+C caught in Run");
                    e.Cancel = true;
                    run      = false;

                    foreach (var job in currentJobs.ToArray())
                    {
                        job.Stop().Wait();
                    }
                }
                else
                {
                    Console.WriteLine("2nd Ctrl+C caught in Run ignoring");
                }
            };

            try
            {
                foreach (var getJob in jobs)
                {
                    if (run)
                    {
                        Stack <Task> tasks = new Stack <Task>(instances);
                        currentJobs = new ConcurrentBag <CantonJob>();

                        for (int i = 0; i < instances; i++)
                        {
                            tasks.Push(Task.Run(() =>
                            {
                                CantonJob job = getJob();
                                currentJobs.Add(job);
                                job.Run();
                            }));
                        }

                        Task.WaitAll(tasks.ToArray());
                    }
                }
            }
            catch (Exception ex)
            {
                Log(ex.ToString(), "canton-job-exceptions.txt");
            }
            finally
            {
                Console.CancelKeyPress -= handler;
            }
        }
Example #2
0
        public static void RunJobs(Queue <CantonJob> jobs)
        {
            CantonJob currentJob = null;
            bool      run        = true;

            ConsoleCancelEventHandler handler = (sender, e) =>
            {
                if (run)
                {
                    Console.WriteLine("Ctrl+C caught in Run");
                    e.Cancel = true;
                    run      = false;

                    if (currentJob != null)
                    {
                        currentJob.Stop().Wait();
                    }
                }
                else
                {
                    Console.WriteLine("2nd Ctrl+C caught in run, ignoring");
                }
            };

            Console.CancelKeyPress += handler;

            try
            {
                foreach (var job in jobs)
                {
                    if (run)
                    {
                        currentJob = job;
                        currentJob.Run();
                        currentJob = null;
                    }
                }
            }
            catch (Exception ex)
            {
                Log(ex.ToString(), "canton-job-exceptions.txt");
            }
            finally
            {
                Console.CancelKeyPress -= handler;
            }
        }