GetInstance() static private method

static private GetInstance ( ) : DelegateWork
return DelegateWork
Example #1
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>
        public Task StartBackground(Action action, Action completionCallback)
        {
            var work = DelegateWork.GetInstance();

            work.Action  = action;
            work.Options = DefaultOptions;
            return(StartBackground(work, completionCallback));
        }
Example #2
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 void Do(Action action1, Action action2)
        {
            var work = DelegateWork.GetInstance();

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

            action1();
            task.Wait();
        }
Example #3
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 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));
        }
Example #4
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 void Do(params Action[] actions)
        {
            List <Task> tasks = taskPool.Get();

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