public ThreadProcessor
            (
                ConcurrentAsyncQueue <T> queue
                , EventWaitHandle wait
            )
            {
                Break  = false;
                Waiter = wait;
                Sender = queue;
                QueuePerformanceCountersContainer qpcc = Sender
                                                         .PerformanceCountersContainer;

                _timerCounters = new WriteableTuple
                                 <
                    bool
                    , Stopwatch
                    , PerformanceCounter
                    , PerformanceCounter
                                 >[]
                {
                    WriteableTuple
                    .Create
                    <
                        bool
                        , Stopwatch
                        , PerformanceCounter
                        , PerformanceCounter
                    >
                    (
                        false
                        , null
                        , qpcc
                        .QueuedWaitAverageTimerPerformanceCounter
                        , qpcc
                        .QueuedWaitAverageBasePerformanceCounter
                    )
                    , WriteableTuple
                    .Create
                    <
                        bool
                        , Stopwatch
                        , PerformanceCounter
                        , PerformanceCounter
                    >
                    (
                        true
                        , null
                        , qpcc
                        .DequeueProcessedAverageTimerPerformanceCounter
                        , qpcc
                        .DequeueProcessedAverageBasePerformanceCounter
                    )
                };
            }
        public void AttachPerformanceCounters
        (
            string instanceNamePrefix
            , string categoryName
            , QueuePerformanceCountersContainer performanceCounters
        )
        {
            var process      = Process.GetCurrentProcess();
            var processName  = process.ProcessName;
            var instanceName = string.Format
                               (
                "{0}-{1}"
                , instanceNamePrefix
                , processName
                               );

            PerformanceCounters = performanceCounters;
            PerformanceCounters
            .AttachPerformanceCountersToProperties(instanceName, categoryName);
            _isAttachedPerformanceCounters = true;
        }
        public void AttachPerformanceCounters
        (
            string categoryName
            , string instanceNamePrefix
            , QueuePerformanceCountersContainer performanceCounters
            , Func <bool> onEnabledCountPerformanceProcessFunc = null
            , PerformanceCounterInstanceLifetime
            performanceCounterInstanceLifetime
            = PerformanceCounterInstanceLifetime.Global
            , long?initializePerformanceCounterInstanceRawValue = null
        )
        {
            var process      = Process.GetCurrentProcess();
            var processName  = process.ProcessName;
            var instanceName = string
                               .Format
                               (
                "{0}-{1}"
                , instanceNamePrefix
                , processName
                               );

            PerformanceCountersContainer = performanceCounters;
            PerformanceCountersContainer
            .AttachPerformanceCountersToMembers
            (
                categoryName
                , instanceName
                , performanceCounterInstanceLifetime
                , initializePerformanceCounterInstanceRawValue
            );
            PerformanceCountersContainer
            .RegisterCountersUsage();
            _isAttachedPerformanceCounters        = true;
            _onEnabledCountPerformanceProcessFunc = onEnabledCountPerformanceProcessFunc;
        }
            public void ThreadProcess()
            {
                long l = 0;

                Interlocked.Increment(ref Sender._concurrentDequeueThreadsCount);
                QueuePerformanceCountersContainer qpcc = Sender.PerformanceCountersContainer;
                var queue            = Sender.InternalQueue;
                var reThrowException = false;

                var enabledCountPerformance = true;

                {
                    if (Sender._onEnabledCountPerformanceProcessFunc != null)
                    {
                        enabledCountPerformance = Sender._onEnabledCountPerformanceProcessFunc();
                    }
                }
                PerformanceCountersHelper
                .TryCountPerformance
                (
                    () =>
                {
                    return(enabledCountPerformance);
                }
                    , reThrowException
                    , qpcc.IncrementCountersBeforeCountPerformanceInThread             //incrementCountersBeforeCountPerformanceInThread
                    , null
                    , null
                    , () =>
                {
                    #region Try Process
                    if (Sender.OnDequeueThreadStart != null)
                    {
                        l = Interlocked.Read(ref Sender._concurrentDequeueThreadsCount);
                        Sender
                        .OnDequeueThreadStart
                        (
                            string
                            .Format
                            (
                                "{0} Threads Count {1},Queue Count {2},Current Thread: {3} at {4}"
                                , "Threads ++ !"
                                , l
                                , queue.Count
                                , Thread.CurrentThread.Name
                                , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff")
                            )
                        );
                    }
                    while (true)
                    {
                        #region while true loop
                        if (Break)
                        {
                            break;
                        }
                        while (!queue.IsEmpty)
                        {
                            #region while queue.IsEmpty loop
                            if (Break)
                            {
                                break;
                            }
                            Tuple <Stopwatch, T> item = null;
                            if (queue.TryDequeue(out item))
                            {
                                Stopwatch stopwatchDequeue = null;
                                Stopwatch stopwatchEnqueue = item.Item1;
                                if (enabledCountPerformance)
                                {
                                    Sender
                                    ._stopwatchsPool
                                    .TryGet(out stopwatchDequeue);
                                }

                                _timerCounters[0].Item2 = stopwatchEnqueue;
                                _timerCounters[1].Item2 = stopwatchDequeue;

                                if (Sender._onEnabledCountPerformanceProcessFunc != null)
                                {
                                    enabledCountPerformance = Sender._onEnabledCountPerformanceProcessFunc();
                                }
                                PerformanceCountersHelper
                                .TryCountPerformance
                                (
                                    () =>
                                {
                                    return(enabledCountPerformance);
                                }
                                    , reThrowException
                                    ,                             //incrementCountersBeforeCountPerformanceForDequeue
                                    qpcc.IncrementCountersBeforeCountPerformanceForDequeue
                                    ,                             //decrementCountersBeforeCountPerformanceForDequeue
                                    qpcc.DecrementCountersBeforeCountPerformanceForDequeue
                                    , _timerCounters
                                    //qpcc.TimerCounters
                                    , () =>                                             //try
                                {
                                    if (Sender.OnDequeue != null)
                                    {
                                        var element = item.Item2;
                                        item        = null;
                                        Sender.OnDequeue(element);
                                    }
                                }
                                    , (x, y, z) =>                                      //catch
                                {
                                    qpcc
                                    .CaughtExceptionsPerformanceCounter
                                    .Increment();
                                    if (Sender.OnDequeueProcessCaughtException != null)
                                    {
                                        reThrowException = Sender
                                                           .OnDequeueProcessCaughtException
                                                           (
                                            Sender
                                            , x
                                            , y
                                            , z
                                                           );
                                    }
                                    if (!reThrowException)
                                    {
                                        if (Sender.OnCaughtException != null)
                                        {
                                            reThrowException = Sender.OnCaughtException(Sender, x, y, z);
                                        }
                                    }
                                    return(reThrowException);
                                }
                                    , null                        //finally
                                    , null
                                    ,                             //incrementCountersAfterCountPerformanceForDequeue
                                    qpcc.IncrementCountersAfterCountPerformanceForDequeue
                                );
                                //池化
                                stopwatchEnqueue.Reset();
                                stopwatchDequeue.Reset();
                                var r = Sender._stopwatchsPool.TryPut(stopwatchDequeue);
                                if (!r)
                                {
                                    stopwatchDequeue.Stop();
                                    stopwatchDequeue = null;
                                }
                                r = Sender._stopwatchsPool.TryPut(stopwatchEnqueue);
                                if (!r)
                                {
                                    stopwatchEnqueue.Stop();
                                    stopwatchEnqueue = null;
                                }
                            }
                            #endregion while queue.IsEmpty loop
                        }
                        #region wait

                        Sender
                        ._waitingDequeueThreadsProcessorsStack
                        .TryPush(this);
                        //Console.WriteLine("Enqueue(this), {0}", Sender._waitingDequeueThreadsProcessorsStack.Count);
                        //Console.WriteLine("Enqueue(this), {0}", Sender._waitingDequeueThreadsProcessorsStack.Count);
                        if (Break)
                        {
                        }
                        if (!Waiter.WaitOne(Sender.DequeueIdleSleepSeconds * 1000))
                        {
                        }
                        #endregion wait
                        #endregion while true loop
                    }
                    #endregion
                }
                    , (x, y, z) =>                              //catch
                {
                    #region Catch Process
                    if (Sender.OnCaughtException != null)
                    {
                        reThrowException = Sender.OnCaughtException(Sender, x, y, z);
                    }
                    return(reThrowException);

                    #endregion
                }
                    , (x, y, z, w) =>                           //finally
                {
                    #region Finally Process
                    l = Interlocked.Decrement(ref Sender._concurrentDequeueThreadsCount);
                    if (l < 0)
                    {
                        Interlocked.Exchange(ref Sender._concurrentDequeueThreadsCount, 0);
                    }
                    if (Sender.OnDequeueThreadEnd != null)
                    {
                        Sender
                        .OnDequeueThreadEnd
                        (
                            string.Format
                            (
                                "{0} Threads Count {1},Queue Count {2},Current Thread: {3} at {4}"
                                , "Threads--"
                                , l
                                , Sender
                                .InternalQueue
                                .Count
                                , Thread
                                .CurrentThread
                                .Name
                                , DateTime
                                .Now
                                .ToString("yyyy-MM-dd HH:mm:ss.fffff")
                            )
                        );
                    }
                    if (!Break)
                    {
                        Sender
                        .StartIncreaseDequeueProcessThreads(1);
                    }
                    Break = false;
                    #endregion
                }
                    ,             //decrementCountersAfterCountPerformanceInThread
                    qpcc.DecrementCountersAfterCountPerformanceInThread
                    ,             //incrementCountersAfterCountPerformanceInThread
                    qpcc.IncrementCountersAfterCountPerformanceInThread
                );
            }
            public void ThreadProcess()
            {
                long l = 0;

                Interlocked.Increment(ref Sender._concurrentDequeueThreadsCount);
                bool counterEnabled = Sender._isAttachedPerformanceCounters;
                QueuePerformanceCountersContainer qpcc = Sender.PerformanceCounters;
                var queue            = Sender.InternalQueue;
                var reThrowException = false;

                PerformanceCounter[] incrementCountersBeforeCountPerformanceForThread = null;
                PerformanceCounter[] decrementCountersAfterCountPerformanceForThread  = null;
                PerformanceCounter[] incrementCountersAfterCountPerformanceForThread  = null;

                if (counterEnabled && qpcc != null)
                {
                    incrementCountersBeforeCountPerformanceForThread =
                        new PerformanceCounter[]
                    {
                        qpcc
                        .DequeueThreadStartPerformanceCounter
                        , qpcc
                        .DequeueThreadsCountPerformanceCounter
                    };
                    decrementCountersAfterCountPerformanceForThread =
                        new PerformanceCounter[]
                    {
                        qpcc.DequeueThreadsCountPerformanceCounter
                    };
                    incrementCountersAfterCountPerformanceForThread =
                        new PerformanceCounter[]
                    {
                        qpcc.DequeueThreadEndPerformanceCounter
                    };
                }

                PerformanceCountersHelper
                .TryCountPerformance
                (
                    counterEnabled
                    , reThrowException
                    , incrementCountersBeforeCountPerformanceForThread
                    , null
                    , null
                    , () =>
                {
                    #region Try Process
                    if (Sender.OnDequeueThreadStart != null)
                    {
                        l = Interlocked.Read(ref Sender._concurrentDequeueThreadsCount);
                        Sender
                        .OnDequeueThreadStart
                        (
                            string
                            .Format
                            (
                                "{0} Threads Count {1},Queue Count {2},Current Thread: {3} at {4}"
                                , "Threads ++ !"
                                , l
                                , queue.Count
                                , Thread.CurrentThread.Name
                                , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff")
                            )
                        );
                    }
                    while (true)
                    {
                        #region while true loop
                        if (Break)
                        {
                            break;
                        }
                        while (!queue.IsEmpty)
                        {
                            #region while queue.IsEmpty loop
                            if (Break)
                            {
                                break;
                            }
                            Tuple <Stopwatch, T> item = null;
                            if (queue.TryDequeue(out item))
                            {
                                Stopwatch stopwatchDequeue = QueuedObjectsPoolManager.StopwatchsPool.Get();
                                PerformanceCounter[] incrementCountersBeforeCountPerformanceForDequeue = null;
                                PerformanceCounter[] decrementCountersBeforeCountPerformanceForDequeue = null;
                                PerformanceCounter[] incrementCountersAfterCountPerformanceForDequeue  = null;
                                Tuple
                                <
                                    bool
                                    , Stopwatch
                                    , PerformanceCounter
                                    , PerformanceCounter
                                >[] timerCounters = null;
                                if (counterEnabled && qpcc != null)
                                {
                                    incrementCountersBeforeCountPerformanceForDequeue =
                                        new PerformanceCounter[]
                                    {
                                        qpcc
                                        .DequeuePerformanceCounter
                                    };
                                    decrementCountersBeforeCountPerformanceForDequeue =
                                        new PerformanceCounter[]
                                    {
                                        qpcc
                                        .QueueLengthPerformanceCounter
                                    };
                                    timerCounters = new Tuple
                                                    <
                                        bool
                                        , Stopwatch
                                        , PerformanceCounter
                                        , PerformanceCounter
                                                    >[]
                                    {
                                        Tuple.Create
                                        <
                                            bool                                                                                                                                                        //before 时是否需要启动
                                            , Stopwatch
                                            , PerformanceCounter
                                            , PerformanceCounter                                                                                                                        //base
                                        >

                                        (
                                            false
                                            , item.Item1
                                            , qpcc
                                            .QueuedWaitAverageTimerPerformanceCounter
                                            , qpcc
                                            .QueuedWaitAverageBasePerformanceCounter
                                        )
                                        , Tuple.Create
                                        <
                                            bool
                                            , Stopwatch
                                            , PerformanceCounter
                                            , PerformanceCounter
                                        >
                                        (
                                            true
                                            , stopwatchDequeue
                                            , qpcc
                                            .DequeueProcessedAverageTimerPerformanceCounter
                                            , qpcc
                                            .DequeueProcessedAverageBasePerformanceCounter
                                        )
                                    };

                                    incrementCountersAfterCountPerformanceForDequeue =
                                        new PerformanceCounter[]
                                    {
                                        qpcc
                                        .DequeueProcessedPerformanceCounter
                                        , qpcc
                                        .DequeueProcessedRateOfCountsPerSecondPerformanceCounter
                                    };
                                }

                                PerformanceCountersHelper
                                .TryCountPerformance
                                (
                                    counterEnabled
                                    , reThrowException
                                    , incrementCountersBeforeCountPerformanceForDequeue
                                    , decrementCountersBeforeCountPerformanceForDequeue
                                    , timerCounters
                                    , () =>                                     //try
                                {
                                    if (Sender.OnDequeue != null)
                                    {
                                        var element = item.Item2;
                                        item        = null;
                                        Sender.OnDequeue(element);
                                    }
                                }
                                    , (x) =>                                    //catch
                                {
                                    reThrowException = false;
                                    return(reThrowException);
                                }
                                    , null                                      //finally
                                    , null
                                    , incrementCountersAfterCountPerformanceForDequeue
                                );
                                //池化
                                stopwatchDequeue.Reset();
                                QueuedObjectsPoolManager.StopwatchsPool.Put(stopwatchDequeue);
                            }
                            #endregion while queue.IsEmpty loop
                        }
                        #region wait
                        Sender
                        ._dequeueThreadsProcessorsPool
                        .Enqueue(this);
                        if (Break)
                        {
                        }
                        if (!Wait.WaitOne(Sender.DequeueIdleSleepSeconds * 1000))
                        {
                        }
                        #endregion wait
                        #endregion while true loop
                    }
                    #endregion
                }
                    , (x) =>                            //catch
                {
                    #region Catch Process
                    if (Sender.OnCaughtException != null)
                    {
                        reThrowException = Sender.OnCaughtException(Sender, x);
                    }
                    return(reThrowException);

                    #endregion
                }
                    , (x, y) =>                 //finally
                {
                    #region Finally Process
                    l = Interlocked.Decrement(ref Sender._concurrentDequeueThreadsCount);
                    if (l < 0)
                    {
                        Interlocked.Exchange(ref Sender._concurrentDequeueThreadsCount, 0);
                    }
                    if (Sender.OnDequeueThreadEnd != null)
                    {
                        Sender
                        .OnDequeueThreadEnd
                        (
                            string.Format
                            (
                                "{0} Threads Count {1},Queue Count {2},Current Thread: {3} at {4}"
                                , "Threads--"
                                , l
                                , Sender.InternalQueue.Count
                                , Thread.CurrentThread.Name
                                , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff")
                            )
                        );
                    }
                    if (!Break)
                    {
                        Sender.StartIncreaseDequeueProcessThreads(1);
                    }
                    Break = false;
                    #endregion
                }
                    , decrementCountersAfterCountPerformanceForThread
                    , incrementCountersAfterCountPerformanceForThread

                );
            }