Ejemplo n.º 1
0
        /// <summary>
        /// Enqueues a method for exection inside the current ThreadPool and returns a <see cref="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 virtual Task RunAsTask <TState>(Action <TState> action, TState state)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var item = new TaskThreadPoolWorkItem <TState>(action, state);

            AddWorkItem(item);
            return(item.Task);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Enqueues a method for exection inside the current ThreadPool and returns a <see cref="Task"/> that represents queued operation
        /// </summary>
        /// <param name="action">Representing the method to execute</param>
        /// <returns>Create Task</returns>
        /// <exception cref="ArgumentNullException">Action is null</exception>
        public virtual Task RunAsTask(Action action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var item = new TaskThreadPoolWorkItem(action);

            AddWorkItem(item);
            return(item.Task);
        }
Ejemplo n.º 3
0
        public void TestTaskThreadPoolWorkItemExecute()
        {
            int wasExecuted = 0;

            var item = new TaskThreadPoolWorkItem(() =>
            {
                Interlocked.Exchange(ref wasExecuted, 1);
            });

            item.Run(false, true);

            Assert.AreEqual(1, wasExecuted);
            Assert.IsTrue(item.Task.IsCompleted);
        }
Ejemplo n.º 4
0
        public void TestTaskThreadPoolWorkItemWithParamExecute()
        {
            int wasExecuted = 0;
            int stateVal    = 0;

            var item = new TaskThreadPoolWorkItem <int>((state) =>
            {
                stateVal = state;
                Interlocked.Exchange(ref wasExecuted, 1);
            }, 100);

            item.Run(false, true);

            Assert.AreEqual(1, wasExecuted);
            Assert.AreEqual(100, stateVal);
            Assert.IsTrue(item.Task.IsCompleted);
        }