

using System;
using System.Threading;

namespace CsvCount_ES
{
    public partial class ThreadPool
    {
        //调用（IWorkItemState）的result属性触发锁，从而在函数执行之前阻塞当前线程。
        //对返回对象立即调用Dispose，以自动重用每个对象后面的数据结构，以避免垃圾回收。
        //在方法执行中可以根据IsCompleted验证属性。IsCompleted实际上触发了WaitOne（1）在（<see cref="ManualResetEvent" /> 内部）因此几乎立即返回。
        /// <param name="workerFunction">The worker function.</param>
        /// <param name="asyncCallback">异步回调处理函数</param>
        /// <returns>
       ///返回（IWorkItemState）作为操作对象，该操作是排队、或者执行。
        /// </returns>
        public IWorkItemState EnqueueWorkItem(Action workerFunction, CallbackFunction asyncCallback = null)
        {
            var workItem = GetWorkItem(asyncCallback);
            workItem.DelegateInputParameters = new object[] { };
            workItem.Delegate = delegateInputParameters =>
            {
                workerFunction.Invoke();
                return null;
            };

            var workItemState = new WorkItemState(workItem.WorkItemStateTypeless);
            EnqueueWorkItemInternal(workItem);
            return workItemState;
        }

        /// <typeparam name="T1">类型</typeparam>
        /// <param name="workerFunction">执行函数.</param>
        /// <param name="arg1">参数1</param>
        /// <param name="asyncCallback">异步回掉函数</param>
        /// <returns>
        ///  返回值：“工作项-状态结构"，该操作是排队、或者执行。
        /// </returns>
        public IWorkItemState EnqueueWorkItem<T1>(Action<T1> workerFunction, T1 arg1,
            CallbackFunction asyncCallback = null)
        {
            var workItem = GetWorkItem(asyncCallback);
            workItem.DelegateInputParameters = new object[] { arg1 };
            workItem.Delegate = delegateInputParameters =>
            {
                workerFunction.Invoke(arg1);
                return null;
            };

            var workItemState = new WorkItemState(workItem.WorkItemStateTypeless);
            EnqueueWorkItemInternal(workItem);
            return workItemState;
        }


        /// <typeparam name="T1">The type of the 1.</typeparam>
        /// <typeparam name="T2">The type of the 2.</typeparam>
        /// <param name="workerFunction">The worker function.</param>
        /// <param name="arg1">The arg1.</param>
        /// <param name="arg2">The arg2.</param>
        /// <param name="asyncCallback">The async callback.</param>
        /// <returns>
        ///     Returns a work-item-state-struct as a handle to the operation that is just about, or queued, to be executed.
        /// </returns>
        public IWorkItemState EnqueueWorkItem<T1, T2>(Action<T1, T2> workerFunction, T1 arg1, T2 arg2,
            CallbackFunction asyncCallback = null)
        {
            var workItem = GetWorkItem(asyncCallback);
            workItem.DelegateInputParameters = new object[] { arg1, arg2 };
            workItem.Delegate = delegateInputParameters =>
            {
                workerFunction.Invoke(arg1, arg2);
                return null;
            };

            var workItemState = new WorkItemState(workItem.WorkItemStateTypeless);
            EnqueueWorkItemInternal(workItem);
            return workItemState;
        }



        /// <typeparam name="T1">The type of the 1.</typeparam>
        /// <typeparam name="T2">The type of the 2.</typeparam>
        /// <typeparam name="T3">The type of the 3.</typeparam>
        /// <param name="workerFunction">The worker function.</param>
        /// <param name="arg1">The arg1.</param>
        /// <param name="arg2">The arg2.</param>
        /// <param name="arg3">The arg3.</param>
        /// <param name="asyncCallback">The async callback.</param>
        /// <returns>
        ///     Returns a work-item-state-struct as a handle to the operation that is just about, or queued, to be executed.
        /// </returns>
        public IWorkItemState EnqueueWorkItem<T1, T2, T3>(Action<T1, T2, T3> workerFunction, T1 arg1, T2 arg2, T3 arg3,
            CallbackFunction asyncCallback = null)
        {
            var workItem = GetWorkItem(asyncCallback);
            workItem.DelegateInputParameters = new object[] { arg1, arg2, arg3 };
            workItem.Delegate = delegateInputParameters =>
            {
                workerFunction.Invoke(arg1, arg2, arg3);
                return null;
            };

            var workItemState = new WorkItemState(workItem.WorkItemStateTypeless);
            EnqueueWorkItemInternal(workItem);
            return workItemState;
        }


        /// <typeparam name="T1">The type of the 1.</typeparam>
        /// <typeparam name="T2">The type of the 2.</typeparam>
        /// <typeparam name="T3">The type of the 3.</typeparam>
        /// <typeparam name="T4">The type of the 4.</typeparam>
        /// <param name="workerFunction">The worker function.</param>
        /// <param name="arg1">The arg1.</param>
        /// <param name="arg2">The arg2.</param>
        /// <param name="arg3">The arg3.</param>
        /// <param name="arg4">The arg4.</param>
        /// <param name="asyncCallback">The async callback.</param>
        /// <returns>
        ///     Returns a work-item-state-struct as a handle to the operation that is just about, or queued, to be executed.
        /// </returns>
        public IWorkItemState EnqueueWorkItem<T1, T2, T3, T4>(Action<T1, T2, T3, T4> workerFunction, T1 arg1, T2 arg2,
            T3 arg3,
            T4 arg4, CallbackFunction asyncCallback = null)
        {
            var workItem = GetWorkItem(asyncCallback);
            workItem.DelegateInputParameters = new object[] { arg1, arg2, arg3, arg4 };
            workItem.Delegate = delegateInputParameters =>
            {
                workerFunction.Invoke(arg1, arg2, arg3, arg4);
                return null;
            };

            var workItemState = new WorkItemState(workItem.WorkItemStateTypeless);
            EnqueueWorkItemInternal(workItem);
            return workItemState;
        }

        /// <typeparam name="T1">The type of the 1.</typeparam>
        /// <typeparam name="T2">The type of the 2.</typeparam>
        /// <typeparam name="T3">The type of the 3.</typeparam>
        /// <typeparam name="T4">The type of the 4.</typeparam>
        /// <typeparam name="T5">The type of the 5.</typeparam>
        /// <param name="workerFunction">The worker function.</param>
        /// <param name="arg1">The arg1.</param>
        /// <param name="arg2">The arg2.</param>
        /// <param name="arg3">The arg3.</param>
        /// <param name="arg4">The arg4.</param>
        /// <param name="arg5">The arg5.</param>
        /// <param name="asyncCallback">The async callback.</param>
        /// <returns>
        ///     Returns a work-item-state-struct as a handle to the operation that is just about, or queued, to be executed.
        /// </returns>
        public IWorkItemState EnqueueWorkItem<T1, T2, T3, T4, T5>(Action<T1, T2, T3, T4, T5> workerFunction, T1 arg1,
            T2 arg2,
            T3 arg3, T4 arg4, T5 arg5, CallbackFunction asyncCallback = null)
        {
            var workItem = GetWorkItem(asyncCallback);
            workItem.DelegateInputParameters = new object[] { arg1, arg2, arg3, arg4, arg5 };
            workItem.Delegate = delegateInputParameters =>
            {
                workerFunction.Invoke(arg1, arg2, arg3, arg4, arg5);
                return null;
            };

            var workItemState = new WorkItemState(workItem.WorkItemStateTypeless);
            EnqueueWorkItemInternal(workItem);
            return workItemState;
        }
    }
}