Example #1
0
        /// <summary>
        /// Enqueues a method for exection inside the current ThreadPool and returns a <see cref="System.Threading.Tasks.Task{TRes}"/> that represents queued operation
        /// </summary>
        /// <typeparam name="TState">Type of the user state object</typeparam>
        /// <typeparam name="TRes">The type of the operation result</typeparam>
        /// <param name="func">Representing the method to execute</param>
        /// <param name="state">State object</param>
        /// <returns>Created Task</returns>
        /// <exception cref="ArgumentNullException">Func is null</exception>
        public sealed override System.Threading.Tasks.Task <TRes> RunAsTask <TState, TRes>(Func <TState, TRes> func, TState state)
        {
            if (func == null)
            {
                throw new ArgumentNullException(nameof(func));
            }
            var workItem = new ParameterizedClosure <TState, TRes>()
            {
                Action = func, State = state
            };

            return(System.Threading.Tasks.Task.Factory.StartNew(ParameterizedClosure <TState, TRes> .RunAction, workItem));
        }
Example #2
0
        /// <summary>
        /// Enqueues a method for exection inside the current ThreadPool and returns a <see cref="System.Threading.Tasks.Task"/> that represents queued operation
        /// </summary>
        /// <typeparam name="TState">Type of the user state object</typeparam>
        /// <param name="action">Representing the method to execute</param>
        /// <param name="state">State object</param>
        /// <returns>Created Task</returns>
        /// <exception cref="ArgumentNullException">Action is null</exception>
        public sealed override System.Threading.Tasks.Task RunAsTask <TState>(Action <TState> action, TState state)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }
            var workItem = new ParameterizedClosure <TState>()
            {
                Action = action, State = state
            };

            return(System.Threading.Tasks.Task.Factory.StartNew(ParameterizedClosure <TState> .RunAction, workItem));
        }