Пример #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
 public ForLoopWork()
 {
     Options = new WorkOptions()
     {
         MaximumThreads = int.MaxValue
     };
 }
Пример #3
0
 public ForEachLoopWork()
 {
     Options = new WorkOptions()
     {
         MaximumThreads = int.MaxValue
     };
     syncLock = new object();
 }
Пример #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>
        /// Creates and starts a task which executes the given function and stores the result for later retrieval.
        /// </summary>
        /// <typeparam name="T">The type of result the function returns.</typeparam>
        /// <param name="function">The function 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 future which represents one execution of the function.</returns>
        public static Future <T> Start <T>(Func <T> function, WorkOptions options, Action completionCallback)
        {
            if (options.MaximumThreads < 1)
            {
                throw new ArgumentOutOfRangeException("options", "options.MaximumThreads cannot be less than 1.");
            }
            var work = FutureWork <T> .GetInstance();

            work.Function = function;
            work.Options  = options;
            var task = Start(work, completionCallback);

            return(new Future <T>(task, work));
        }
Пример #6
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);
        }
Пример #7
0
 public ActionWork(Action <WorkData> action, WorkOptions options)
 {
     this.Action  = action;
     this.Options = options;
 }
Пример #8
0
 /// <summary>
 /// Creates and starts a task which executes the given function and stores the result for later retrieval.
 /// </summary>
 /// <typeparam name="T">The type of result the function returns.</typeparam>
 /// <param name="function">The function to execute in parallel.</param>
 /// <param name="options">The work options to use with this action.</param>
 /// <returns>A future which represents one execution of the function.</returns>
 public static Future <T> Start <T>(Func <T> function, WorkOptions options)
 {
     return(Start <T>(function, options, null));
 }
Пример #9
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>
 /// <returns>A task which represents one execution of the work.</returns>
 public static Task Start(Action action, WorkOptions options)
 {
     return(Start(action, options, null));
 }
Пример #10
0
 public ActionWork(Action<WorkData> action, WorkOptions options)
 {
     this._Action = action;
     this.Options = options;
 }
Пример #11
0
 public ActionWork(Action action, WorkOptions options)
 {
     this.Action = action;
     this.Options = options;
 }