예제 #1
0
        /// <summary>
        /// Creates and schedules a task to execute the given work with the given work data.
        /// </summary>
        /// <param name="action">The work to execute in parallel.</param>
        /// <param name="completionCallback">A method which will be called in Parallel.RunCallbacks() once this task has completed.</param>
        /// <param name="workData">Data to be passed along both the work and the completion callback.</param>
        /// <returns>A task which represents one execution of the action.</returns>
        public static Task Start(Action <WorkData> action, Action <WorkData> completionCallback, WorkData workData)
        {
            WorkOptions options = new WorkOptions()
            {
                MaximumThreads = 1, DetachFromParent = false, QueueFIFO = false
            };

            var work = DelegateWork.GetInstance();

            work.DataAction = action;
            work.Options    = options;

            var workItem = WorkItem.Get(Thread.CurrentThread);

            workItem.CompletionCallbacks = CallbackBuffer;
            workItem.DataCallback        = completionCallback;

            if (workData != null)
            {
                workItem.WorkData  = workData;
                workData.WorkState = WorkData.WorkStateEnum.NOT_STARTED;
            }
            else
            {
                workItem.WorkData = new WorkData();
            }

            var task = workItem.PrepareStart(work);

            Scheduler.Schedule(task);
            return(task);
        }
예제 #2
0
        /// <summary>
        /// Starts a task in a secondary worker thread. Intended for long running, blocking work such as I/O.
        /// </summary>
        /// <param name="action">The work to execute.</param>
        /// <param name="completionCallback">A method which will be called in Parallel.RunCallbacks() once this task has completed.</param>
        /// <param name="workData">Data to be passed along both the work and the completion callback.</param>
        /// <returns>A task which represents one execution of the action.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="action"/> is <see langword="null"/>.
        /// </exception>
        public static Task StartBackground(Action <WorkData> action, Action <WorkData> completionCallback, WorkData workData)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            var work = DelegateWork.GetInstance();

            work.DataAction = action;
            work.Options    = DefaultOptions;

            var workItem = WorkItem.Get(Thread.CurrentThread);

            workItem.DataCallback = completionCallback;
            if (workData != null)
            {
                workItem.WorkData = workData;
            }
            else
            {
                workItem.WorkData = new WorkData();
            }

            var task = workItem.PrepareStart(work);

            BackgroundWorker.StartWork(task);
            return(task);
        }
예제 #3
0
        /// <summary>
        /// Executes the given work items potentially in parallel with each other.
        /// This method will block until all work is completed.
        /// </summary>
        /// <param name="action1">The work to execute.</param>
        /// <param name="action2">The work to execute.</param>
        public static void Do(Action action1, Action action2)
        {
            var work = DelegateWork.GetInstance();

            work.Action  = action2;
            work.Options = DefaultOptions;
            var task = Start(work);

            action1();
            task.Wait();
        }
예제 #4
0
        /// <summary>
        /// Creates and starts a task to execute the given work.
        /// </summary>
        /// <param name="action">The work to execute in parallel.</param>
        /// <param name="options">The work options to use with this action.</param>
        /// <param name="completionCallback">A method which will be called in Parallel.RunCallbacks() once this task has completed.</param>
        /// <returns>A task which represents one execution of the work.</returns>
        public static Task Start(Action action, WorkOptions options, Action completionCallback)
        {
            if (options.MaximumThreads < 1)
            {
                throw new ArgumentOutOfRangeException("options", "options.MaximumThreads cannot be less than 1.");
            }
            var work = DelegateWork.GetInstance();

            work.Action  = action;
            work.Options = options;
            return(Start(work, completionCallback));
        }
예제 #5
0
        /// <summary>
        /// Starts a task in a secondary worker thread. Intended for long running, blocking, work
        /// such as I/O.
        /// </summary>
        /// <param name="action">The work to execute.</param>
        /// <param name="completionCallback">A method which will be called in Parallel.RunCallbacks() once this task has completed.</param>
        /// <returns>A task which represents one execution of the action.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="action"/> is <see langword="null"/>.
        /// </exception>
        public static Task StartBackground(Action action, Action completionCallback)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            var work = DelegateWork.GetInstance();

            work.Action  = action;
            work.Options = DefaultOptions;
            return(StartBackground(work, completionCallback));
        }
예제 #6
0
        /// <summary>
        /// Executes the given work items potentially in parallel with each other.
        /// This method will block until all work is completed.
        /// </summary>
        /// <param name="actions">The work to execute.</param>
        public static void Do(params Action[] actions)
        {
            List <Task> tasks = taskPool.Get(Thread.CurrentThread);

            for (int i = 0; i < actions.Length; i++)
            {
                var work = DelegateWork.GetInstance();
                work.Action  = actions[i];
                work.Options = DefaultOptions;
                tasks.Add(Start(work));
            }

            for (int i = 0; i < actions.Length; i++)
            {
                tasks[i].Wait();
            }

            tasks.Clear();
            taskPool.Return(Thread.CurrentThread, tasks);
        }
예제 #7
0
        /// <summary>
        /// Creates and schedules a task to execute on the given work-tracking thread.
        /// If the requested thread that does not execute completion callbacks the callback will never be called.
        /// </summary>
        /// <param name="action">The work to execute in parallel.</param>
        /// <param name="workData">Data to be passed along both the work and the completion callback.</param>
        /// <param name="thread">Thread to execute the callback on. If not provided this is the calling thread.</param>
        /// <returns>A task which represents one execution of the action.</returns>
        public static Task ScheduleForThread(Action <WorkData> action, WorkData workData, Thread thread = null)
        {
            if (thread == null)
            {
                thread = Thread.CurrentThread;
            }

            WorkOptions options = new WorkOptions()
            {
                MaximumThreads = 1, DetachFromParent = false, QueueFIFO = false
            };

            var work = DelegateWork.GetInstance();

            work.Options = options;

            var workItem = WorkItem.Get(thread);

            lock (Buffers)
            {
                workItem.CompletionCallbacks = Buffers[thread];
            }
            workItem.DataCallback = action;


            if (workData != null)
            {
                workItem.WorkData = workData;
            }
            else
            {
                workItem.WorkData = new WorkData();
            }

            var task = workItem.PrepareStart(work, thread);

            CallbackBuffer.Add(workItem);
            return(task);
        }