Example #1
0
        static void Main(string[] args)
        {
            const int size = 10000;

            var scheduler = new PipelineScheduler
                            (
                onComplete: OnComplete,
                threadScheduler: new StandardTaskRunner(),
                taskBufferSize: size,
                useTaskSchedulingOffloading: false,
                scheduler: new FcfsScheduler()
                            );

            _finishedDates = new ConcurrentBag <DateTime>();
            var startDate = DateTime.Now;

            scheduler.Start();

            for (int i = 0; i < size; i++)
            {
                scheduler.Push(() => { });
            }

            Console.WriteLine("Press any key to get duration...\n");
            Console.ReadKey();
            scheduler.Dispose();

            var duration = _finishedDates.Max() - startDate;

            Console.WriteLine("Duration | h:{0} m:{1} s:{2} ms: {3}", duration.Hours, duration.Minutes, duration.Seconds, duration.Milliseconds);
            Console.WriteLine("Total: {0}\n", _finishedDates.Count);

            Console.WriteLine("Press any key to exit...\n");
            Console.ReadKey();
        }
Example #2
0
        static void Main(string[] args)
        {
            var       scheduler = new PipelineScheduler(6);
            const int taskCount = 10;

            scheduler.Start();

            // Use delegate / lambda expression task
            Console.WriteLine("Running {0} tasks using lambda task & default settings including First Come First Serve scheduling.\n", taskCount);
            for (int i = 0; i < taskCount; i++)
            {
                int taskId = i;
                scheduler.Push(() =>
                {
                    Interlocked.Increment(ref _activeTasks);
                    var delay = Randomizer.Next(3000);
                    Console.WriteLine("Starting task {0}, {1} with duration of {2}ms. Thread: {3}. Active: {4}.", taskId, DateTime.Now.ToString("hh:mm:ss.fff"), delay, Thread.CurrentThread.ManagedThreadId, _activeTasks);
                    Thread.Sleep(delay);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Task {0} finished at {1}.", taskId, DateTime.Now.ToString("hh:mm:ss.fff"));
                    Console.ResetColor();
                    Interlocked.Decrement(ref _activeTasks);
                });
            }

            Console.WriteLine("Application/Process (main thread) finished. Press any key to exit...\n");
            Console.ReadKey();

            scheduler.Dispose();
        }
Example #3
0
        static void Main(string[] args)
        {
            var sw = new Stopwatch();

            // Create a pipeline scheduler supporting 6 concurrent threads
            // Also support FCFS scheduling
            var       scheduler = new PipelineScheduler(6);
            const int taskCount = 10;

            // Use IPipelineTask interface to define work.
            // This allows extensive custom configuration for your task.

            Console.WriteLine("Running {0} tasks using default settings including First Come First Serve scheduling.\n", taskCount);
            long elapsedMs    = 0;
            long elapsedTicks = 0;

            scheduler.Start();
            for (int i = 0; i < taskCount; i++)
            {
                sw.Start();
                scheduler.Push(null, new CustomTask(i));
                sw.Stop();

                Console.WriteLine("Task started in {0}ms or {1} ticks", sw.ElapsedMilliseconds - elapsedMs, sw.ElapsedTicks - elapsedTicks);
                elapsedMs    = sw.ElapsedMilliseconds;
                elapsedTicks = sw.ElapsedTicks;
            }

            Console.WriteLine("Application/Process (main thread) finished. Press any key to exit...\n");
            Console.ReadKey();

            scheduler.Dispose();
        }
Example #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting main application.");
            Console.WriteLine("Total Tasks: {0}", TotalTasks);

            // Create the pipeline scheduler
            var pipe = new PipelineScheduler();

            // Create the tasks first. Strictly this
            // approach is not necessary. The task can
            // be created and launched at the same time.
            var tasks = new List <Action>();

            for (int i = 1; i < TotalTasks + 1; i++)
            {
                var taskNumber = i;
                tasks.Add(() => MyTask(taskNumber, ProgressUpdate));
            }

            // Push tasks into the pipeline.
            for (int i = 0; i < TotalTasks; i++)
            {
                pipe.Push(tasks[i]);
            }

            pipe.Start();
            Console.SetCursorPosition(0, 6);
            Console.WriteLine("Press any key to quit...");
            Console.ReadKey();
            pipe.Dispose();
        }
Example #5
0
        static void Main(string[] args)
        {
            var sw = new Stopwatch();

            // Create a pipeline scheduler using FCFS scheduling
            var scheduler = new PipelineScheduler
                            (
                Environment.ProcessorCount,                     // Match pipeline count (thread limit) with the number of cores
                new ThreadPoolTaskRunner(),                     // Use ThreadPool threads (suitable for non-web .NET apps)
                abortLongRunningTasks: true,                    // Set long running tasks to be aborted
                abortTaskTimeoutIntervalInMilliseconds: 500,    // Set task timeout monitor interval to 500 milliseconds (1/2 second)

                // Custom handler for exceptions that occur inside the task
                onException: TaskMonitorExceptionLog
                            );

            long      elapsedMs    = 0;
            long      elapsedTicks = 0;
            const int taskCount    = 15;

            Console.WriteLine("Running {0} tasks using default settings including First Come First Serve scheduling.\n", taskCount);

            scheduler.Start();
            for (int i = 0; i < taskCount; i++)
            {
                sw.Start();

                // Use IPipelineTask interface to define work allowing
                // extensive custom configuration for your task.
                // Push the task into the pipeline queue for execution.
                scheduler.Push
                (
                    new CustomTask(i)
                {
                    // Set this task to be terminated if it takes
                    // longer than 1 second to complete.
                    ThreadAbortTimeout = TimeSpan.FromSeconds(1)
                }
                );

                // Push another task using a simple lambda expression
                // without having to define a custom IPipelineTask.
                // Powerful but without the custom configuration available
                // with an IPipelineTask.
                scheduler.Push(() => { /* Do some work here */ });

                sw.Stop();

                Console.WriteLine("{0}ms, {1} ticks", sw.ElapsedMilliseconds - elapsedMs, sw.ElapsedTicks - elapsedTicks);
                elapsedMs    = sw.ElapsedMilliseconds;
                elapsedTicks = sw.ElapsedTicks;
            }

            Console.WriteLine("Application/Process (main thread) finished. Press any key to exit...\n");
            Console.ReadKey();

            scheduler.Dispose();
        }
Example #6
0
        static void Main(string[] args)
        {
            var scheduler = new PipelineScheduler
                            (
                onException: OnFault
                            );

            using (scheduler)
            {
                scheduler.Push(new SolveProblem());

                scheduler.Start();

                Console.WriteLine("Press any key to continue...\n");
                Console.ReadKey();
            }
        }
Example #7
0
        static void Main(string[] args)
        {
            var       scheduler = new PipelineScheduler(6, scheduler: new PriorityScheduler(TimeSpan.FromMilliseconds(500)));
            const int taskCount = 10;

            scheduler.Start();

            // Priority scheduling
            Console.WriteLine("Running {0} tasks using default settings with priority scheduling.\n", taskCount);
            for (int i = 0; i < taskCount; i++)
            {
                scheduler.Push(new CustomTask(i));
            }

            Console.WriteLine("Application/Process (main thread) finished. Press any key to exit...\n");
            Console.ReadKey();

            scheduler.Dispose();
        }
Example #8
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting main application.");
            Console.WriteLine("Thread Id: {0}", Thread.CurrentThread.ManagedThreadId);

            var pipe = new PipelineScheduler(threadScheduler: new ThreadPoolTaskRunner());

            for (int i = 0; i < 10; i++)
            {
                var task = new SendMessageTask(i.ToString());
                pipe.Push(task);
            }

            pipe.Start();
            Console.WriteLine("Press any key to quit...");
            Console.ReadKey();
            pipe.Stop();
            pipe.Dispose();
        }
Example #9
0
        static void Main(string[] args)
        {
            var scheduler = new PipelineScheduler
                            (
                abortLongRunningTasks: true,
                abortTaskTimeoutIntervalInMilliseconds: 100,
                onException: OnFault,
                onCancel: OnCancel
                            );

            using (scheduler)
            {
                scheduler.Push(new SolveProblem(OnFault));

                scheduler.Start();

                Console.WriteLine("Press any key to continue...\n");
                Console.ReadKey();
            }
        }
Example #10
0
        static void Main(string[] args)
        {
            for (int pass = 0; pass < 2; pass++)
            {
                Console.WriteLine("Pass {0}", pass + 1);

                var       sw            = new Stopwatch();
                const int totalMessages = 1000;
                var       signal        = new AutoResetEvent(false);
                var       syncObj       = new object();
                const int pipelines     = 1;

                List <string> pushQueue          = new List <string>(totalMessages);
                List <string> pullQueue          = new List <string>(totalMessages);
                long          queueDuration      = 0;
                long          dequeueDuration    = 0;
                int           pushCounter        = 0;
                int           pullCounter        = 0;
                int           pullFailureCounter = 0;
                int           pushFailureCounter = 0;
                var           queueName          = "PipelineQueue-V2-" + Guid.NewGuid();

                // Create queue
                using (AmazonSqs.ObjectQueue queue = new AmazonSqs.ObjectQueue(
                           ConfigurationManager.AppSettings["AWSAccessKey"],              // Or your AWS access key
                           ConfigurationManager.AppSettings["AWSSecretKey"],              // Or your AWS secret key
                           RegionEndpoint.EUWest1,
                           queueName
                           ))
                {
                    // Push messages
                    using (var scheduler = new PipelineScheduler(new ThreadPoolTaskRunner(),
                                                                 onException: (task, e) =>
                    {
                        pushCounter++;
                        pushFailureCounter++;
                    },
                                                                 onComplete: task =>
                    {
                        lock (syncObj)
                        {
                            pushCounter++;

                            if (pushCounter == totalMessages)
                            {
                                sw.Stop();
                                queueDuration = sw.ElapsedMilliseconds;
                                signal.Set();
                            }
                        }
                    }))
                    {
                        Console.WriteLine("Starting pushing to the queue.");
                        sw.Start();
                        scheduler.Start();

                        for (int i = 0; i < totalMessages; i++)
                        {
                            scheduler.Push(() =>
                            {
                                var s = Guid.NewGuid().ToString();
                                pushQueue.Add(s);
                                queue.Enqueue(new List <string>(new[] { s }));
                            });
                        }

                        signal.WaitOne();
                    }
                }

                using (AmazonSqs.ObjectQueue queue = new AmazonSqs.ObjectQueue(
                           ConfigurationManager.AppSettings["AWSAccessKey"],              // Or your AWS access key
                           ConfigurationManager.AppSettings["AWSSecretKey"],              // Or your AWS secret key
                           RegionEndpoint.EUWest1,
                           queueName
                           ))
                {
                    using (var scheduler = new PipelineScheduler(new ThreadPoolTaskRunner(),
                                                                 onException: (task, e) =>
                    {
                        pullCounter++;
                        pullFailureCounter++;
                    },
                                                                 onComplete: task =>
                    {
                        lock (syncObj)
                        {
                            pullCounter++;

                            if (pullCounter == pushCounter)
                            {
                                sw.Stop();
                                dequeueDuration = sw.ElapsedMilliseconds;
                                signal.Set();
                            }
                        }
                    }))
                    {
                        Console.WriteLine("Starting pulling from the queue.");
                        sw.Restart();
                        scheduler.Start();

                        // Dequeue messages
                        for (int i = 0; i < totalMessages; i++)
                        {
                            scheduler.Push(() =>
                            {
                                var response = queue.Dequeue <List <string> >();
                                pullQueue.Add(response.FirstOrDefault()?.FirstOrDefault());
                            });
                        }

                        signal.WaitOne();
                    }

                    // Remove queue
                    queue.DeleteQueue();
                }

                // Show stats
                Console.WriteLine("Total messages: {0}", totalMessages);
                Console.WriteLine("Push duration: {0}", queueDuration);
                Console.WriteLine("Pull duration: {0}", dequeueDuration);
                Console.WriteLine("Push Counter: {0}", pushCounter);
                Console.WriteLine("Pull Counter: {0}", pullCounter);
                Console.WriteLine("Push Failure Counter: {0}", pushFailureCounter);
                Console.WriteLine("Pull Failure Counter: {0}", pullFailureCounter);
                Console.WriteLine("Push/Pull Intersect: {0}", pushQueue.Intersect(pullQueue).Count());
                Console.WriteLine();
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }