FlushTasks() public method

Wait for all currenty added tasks to finish executing and continue waiting for tasks
public FlushTasks ( ) : void
return void
        public void ParallelRunnerTestTaskCount()
        {
            using (TaskExecutor runner = new TaskExecutor())
            {
                const int count = 10;

                foreach (var num in Enumerable.Range(0, count))
                {
                    runner.AddTask(() => Thread.Sleep(100));
                }

                runner.TotalAddedTasks.Should().Be(count);
                runner.RemainingTasks.Should().Be(count);
                runner.ErrorCount.Should().Be(0);
                runner.FinishedTasksCount.Should().Be(0);

                runner.FlushTasks();

                runner.TotalAddedTasks.Should().Be(count);
                runner.RemainingTasks.Should().Be(0);
                runner.FinishedTasksCount.Should().Be(count);
                runner.ErrorCount.Should().Be(0);

                runner.Finish();

                runner.TotalAddedTasks.Should().Be(count);
                runner.RemainingTasks.Should().Be(0);
                runner.FinishedTasksCount.Should().Be(count);
                runner.ErrorCount.Should().Be(0);
            }
        }
        public void ParallelRunnerFlushTasksTest()
        {
            for (int count = 0; count < 20; count++)
            {
                using (TaskExecutor runner = new TaskExecutor())
                {
                    int i = 0;
                    double r;

                    foreach (var num in Enumerable.Range(0, count))
                    {
                        runner.AddTask(() =>
                        {
                            for (int j = 0; j < 1000; j++)
                            {
                                r = Math.Sin(0.234234) * Math.Atan(j);
                            }
                            Interlocked.Increment(ref i);
                        });
                    }

                    (i <= count).Should().BeTrue();

                    runner.FlushTasks();

                    i.Should().Be(count);

                    runner.Finish();

                    i.Should().Be(count);
                }
            }
        }
        public void ParallelRunnerTestMultipleThreads()
        {
            using (TaskExecutor runner = new TaskExecutor(5))
            {

                int times = 1000;
                int taskCount = 10000;
                int currentTask = 0;
                int val = 0;

                Action task = null;
                task = () =>
                {
                    Interlocked.Increment(ref currentTask);
                    Interlocked.Increment(ref val);
                    if (currentTask <= taskCount)
                        runner.AddTask(task);
                    Thread.SpinWait((taskCount % 30) * 10000);
                };


                Enumerable.Range(0, times).ToList().ForEach(n => runner.AddTask(task));

                runner.FlushTasks();

                val.Should().Be(taskCount + times);
            }
        }
        public void ParallelRunnerStressTest()
        {
            // 1000 increments with 2ms sleep each on 2 threads
            // 1000 * 2ms / 2threads = 1000ms min wait

            using (TaskExecutor runner = new TaskExecutor(2))
            {

                int val = 0;

                Enumerable.Range(0, 1000).ToList().ForEach(n => runner.AddTask(() => { Thread.Sleep(2); Interlocked.Increment(ref val); }));

                runner.FlushTasks();

                val.Should().Be(1000);
                runner.Finish();
            }
        }
        public void ParallelRunnerFlushTestThreadSleep()
        {
            using (TaskExecutor runner = new TaskExecutor())
            {
                int i = 0;

                foreach (var n in Enumerable.Range(0, 10))
                {
                    runner.AddTask(() =>
                    {
                        if (n % 2 == 0)
                        {
                            Thread.Sleep(100);
                        }
                        Interlocked.Increment(ref i);
                    });
                }

                (i <= 10).Should().BeTrue();

                runner.FlushTasks();

                i.Should().Be(10);

                runner.Finish();

                i.Should().Be(10);
            }
        }
        public void ParallelRunnerFlushTestAutoThreads()
        {
            using (TaskExecutor runner = new TaskExecutor())
            {
                int i = 0;

                const int count = 10;

                foreach (var n in Enumerable.Range(0, count))
                {
                    runner.AddTask(() =>
                    {
                        if (n % 2 == 0)
                        {
                            Thread.SpinWait(int.MaxValue / 200);
                        }
                        Interlocked.Increment(ref i);
                    });
                }

                (i <= count).Should().BeTrue();

                runner.FlushTasks();

                i.Should().Be(count);

                runner.Finish();

                i.Should().Be(count);
            }
        }