public T GetResult() { if (_work == null || _work.ID != _id) { throw new InvalidOperationException("The result of a Task<T> can only be retrieved once."); } _task.Wait(); var result = _work.Result; _work.Recycle(); _work = null; return(result); }
/// <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 <see cref="RunCallbacks"/> once this task has completed. /// </param> /// <returns>A <see cref="Task{T}"/> which stores the result of the function.</returns> /// <remarks> /// <strong>Important:</strong> The completion callback is not executed automatically. Instead, /// the callback is only executed when <see cref="RunCallbacks"/> is called. See /// <see cref="RunCallbacks"/> for additional information. /// </remarks> /// <exception cref="ArgumentNullException"> /// <paramref name="function"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// Invalid number of maximum threads set in <see cref="IWork.Options"/>. /// </exception> public static Task<T> Start<T>(Func<T> function, WorkOptions options, Action completionCallback) { if (options.MaximumThreads < 1) throw new ArgumentException("options.MaximumThreads cannot be less than 1.", "options"); var work = FutureWork<T>.Create(); work.Function = function; work.Options = options; var task = Start(work, completionCallback); return new Task<T>(task, work); #else throw new NotSupportedException("Unity builds do not yet support multithreading."); }
internal Task(Task task, FutureWork <T> work) { _task = task; _work = work; _id = work.ID; }