

namespace CsvCount_ES
{
    /// <summary>
    ///     接口
    /// </summary>
    /// <typeparam name="T">T.</typeparam>
    public struct WorkItemState<T> : IWorkItemState<T>
    {
        private bool disposed;
        private readonly WorkItemStateTypeless workItemStateTypeless;

        /// <summary>
        ///    获取一个值，指示该实例是否完成。
        /// </summary>
        /// <value>
        ///     <c>true</c>  <c>false</c>.
        /// </value>
        public bool IsStopped => workItemStateTypeless.IsStopped;

        /// <summary>
        ///   result属性操作
        /// </summary>
        /// <value></value>
        public T Result => (T) workItemStateTypeless.Result;

        /// <summary>
        ///   实例化<see cref="WorkItemState{TResult}" /> .
        /// </summary>
        /// <param name="workItemStateTypeless">工作单元状态</param>
        public WorkItemState(WorkItemStateTypeless workItemStateTypeless)
        {
            this.workItemStateTypeless = workItemStateTypeless;
            disposed = false;
        }

        /// <summary>
        ///    释放
        /// </summary>
        /// <param name="disposing">
        ///     <c>true</c> 托管非托管 <c>false</c> 非托管
        /// </param>
        public void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    workItemStateTypeless.WorkItem.SingleThreadRunner.ThreadPool.ReturnWorkItem(
                        workItemStateTypeless.WorkItem);
                }
                disposed = true;
            }
        }

        /// <summary>
        /// 释放
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
        }
    }

    /// <summary>
    ///    接口 <c>IWorkItemStateTypeless</c> .
    /// </summary>
    public struct WorkItemState : IWorkItemState
    {
        private bool disposed;
        private readonly WorkItemStateTypeless workItemStateTypeless;

        /// <summary>
        ///     获取一个值，指示该实例是否完成。
        /// </summary>
        /// <value>
        ///    
        /// </value>
        public bool IsStopped => workItemStateTypeless.IsStopped;

        /// <summary>
        ///    实例化 <see cref="WorkItemState{TResult}" /> .
        /// </summary>
        /// <param name="workItemStateTypeless">工作单元.</param>
        public WorkItemState(WorkItemStateTypeless workItemStateTypeless)
        {
            this.workItemStateTypeless = workItemStateTypeless;
            disposed = false;
        }

        /// <summary>
        ///    阻塞操作等待工作项完成。销毁前使用。平时不使用。
        /// </summary>
        /// <value>.</value>
        public void Result()
        {
#pragma warning disable 168
            var o = workItemStateTypeless.Result;
#pragma warning restore 168
        }

        /// <summary>
        ///   释放
        /// </summary>
        /// <param name="disposing">
        /// </param>
        public void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    workItemStateTypeless.WorkItem.SingleThreadRunner.ThreadPool.ReturnWorkItem(
                        workItemStateTypeless.WorkItem);
                }
                disposed = true;
            }
        }

        /// <summary>
        /// 释放
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
        }
    }
}