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();
        }
        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();
        }
        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();
        }
        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();
        }
        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();
        }
        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();
        }
Beispiel #7
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();
        }