Пример #1
0
        /// <summary>
        /// Creates an starts a task which executes the given function and stores the resuly for later retreval.
        /// </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 represults one execution of the function.</returns>
        public 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));
        }
Пример #2
0
        /// <summary>
        /// Gets the result. Blocks the calling thread until the future has completed execution.
        /// This can only be called once!
        /// </summary>
        /// <returns></returns>
        public T GetResult()
        {
            if (work == null || work.ID != id)
            {
                throw new InvalidOperationException("The result of a future can only be retrieved once.");
            }

            task.Wait();
            var result = work.Result;

            work.ReturnToPool();
            work = null;

            return(result);
        }
Пример #3
0
 internal Future(Task task, FutureWork <T> work)
 {
     this.task = task;
     this.work = work;
     this.id   = work.ID;
 }