Exemplo n.º 1
0
 public static void Reset()
 {
     lock (_poolLock)
     {
         try
         {
             foreach (object current in _waitingCallbacks)
             {
                 WaitingCallback waitingCallback = (WaitingCallback)current;
                 if (waitingCallback.State is IDisposable)
                 {
                     ((IDisposable)waitingCallback.State).Dispose();
                 }
             }
         }
         catch
         {
         }
         try
         {
             foreach (Thread thread in _workerThreads)
             {
                 if (thread != null)
                 {
                     thread.Abort("reset");
                 }
             }
         }
         catch
         {
         }
         Initialize();
     }
 }
Exemplo n.º 2
0
        /// <summary>Empties the work queue of any queued work items.</summary>
        public static void EmptyQueue()
        {
            lock (_waitingCallbacks.SyncRoot)
            {
                try
                {
                    // Try to dispose of all remaining state
                    foreach (object obj in _waitingCallbacks)
                    {
                        WaitingCallback callback = (WaitingCallback)obj;
                        if (callback.State is IDisposable)
                        {
                            ((IDisposable)callback.State).Dispose();
                        }
                    }
                }
                catch
                {
                    // Make sure an error isn't thrown.
                }

                // Clear all waiting items and reset the number of worker threads currently needed
                // to be 0 (there is nothing for threads to do)
                _waitingCallbacks.Clear();
                _workerThreadNeeded.Reset(0);
            }
        }
Exemplo n.º 3
0
        /// <summary>Empties the work queue of any queued work items.  Resets all threads in the pool.</summary>
        public static void Reset()
        {
            lock (_poolLock) {
                // Cleanup any waiting callbacks
                try {
                    // Try to dispose of all remaining state
                    foreach (object obj in _waitingCallbacks)
                    {
                        WaitingCallback callback = (WaitingCallback)obj;
                        if (callback.State is IDisposable)
                        {
                            ((IDisposable)callback.State).Dispose();
                        }
                    }
                } catch {
                }

                // Shutdown all existing threads
                try {
                    foreach (Thread thread in _workerThreads)
                    {
                        if (thread != null)
                        {
                            thread.Abort("reset");
                        }
                    }
                } catch {
                }

                // Reinitialize the pool (create new threads, etc.)
                Initialize();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// A thread worker function that processes items from the work queue.
        /// </summary>
        private static void ProcessQueuedItems()
        {
            // Process indefinitely
            while (true)
            {
                _workerThreadNeeded.WaitOne();

                // Get the next item in the queue.  If there is nothing there, go to sleep
                // for a while until we're woken up when a callback is waiting.
                WaitingCallback callback = null;

                // Try to get the next callback available.  We need to lock on the
                // queue in order to make our count check and retrieval atomic.
                lock (_poolLock)
                {
                    if (_waitingCallbacks.Count > 0)
                    {
                        try { callback = (WaitingCallback)_waitingCallbacks.Dequeue(); }
                        catch { } // make sure not to fail here
                    }
                }

                if (callback != null)
                {
                    // We now have a callback.  Execute it.  Make sure to accurately
                    // record how many callbacks are currently executing.
                    try
                    {
                        Interlocked.Increment(ref _inUseThreads);
                        callback.Callback(callback.State);
                    }
#if NET1
                    catch (Exception exc)
                    {
                        //暂时注释下面的代码
                        try
                        {
                            UnhandledExceptionEventHandler handler = UnhandledException;
                            if (handler != null)
                            {
                                handler(typeof(ManagedThreadPool), new UnhandledExceptionEventArgs(exc, false));
                            }
                        }
                        catch {}
                    }
#else
                    catch
                    {
                        ;
                    }
#endif

                    finally
                    {
                        Interlocked.Decrement(ref _inUseThreads);
                    }
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>Queues a user work item to the thread pool.</summary>
        /// <param name="callback">
        /// A WaitCallback representing the delegate to invoke when the thread in the
        /// thread pool picks up the work item.
        /// </param>
        /// <param name="state">
        /// The object that is passed to the delegate when serviced from the thread pool.
        /// </param>
        /// <param name="priority">The priority for this callback.</param>
        public static void QueueUserWorkItem(WaitCallback callback, object state, int priority)
        {
            // Create a waiting callback that contains the delegate and its state.
            // At it to the processing queue, and signal that data is waiting.
            WaitingCallback waiting = new WaitingCallback(callback, state);

            lock (_waitingCallbacks.SyncRoot) { _waitingCallbacks.Enqueue(priority, waiting); }
            _workerThreadNeeded.AddOne();
        }
Exemplo n.º 6
0
        /// <summary>将用户工作项排入线程池.</summary>
        /// <param name="callback">
        /// WaitCallback表示在线程中调用时委托的委托
        ///线程池选取工作项。
        /// </param>
        /// <param name="state">
        /// 从线程池提供服务时传递给委托的对象.
        /// </param>
        public static void QueueUserWorkItem(WaitCallback callback, object state)
        {
            //创建一个包含委托及其状态的等待回调。
            //在它处理队列,并发出数据正在等待的信号。
            WaitingCallback waiting = new WaitingCallback(callback, state);

            lock (_poolLock) { _waitingCallbacks.Enqueue(waiting); }
            _workerThreadNeeded.AddOne();
        }
Exemplo n.º 7
0
        public static void QueueUserWorkItem(WaitCallback callback, object state)
        {
            WaitingCallback obj = new WaitingCallback(callback, state);

            lock (_poolLock)
            {
                _waitingCallbacks.Enqueue(obj);
            }
            _workerThreadNeeded.AddOne();
        }
Exemplo n.º 8
0
        /// <summary>
        /// Queues a user work item to the thread pool.
        /// </summary>
        /// <param name="callback">
        /// A WaitCallback representing the delegate to invoke when the thread in the
        /// thread pool picks up the work item.
        /// </param>
        /// <param name="state">
        /// The object that is passed to the delegate when serviced from the thread pool.
        /// </param>
        public void QueueUserWorkItem(WaitCallback callback, object state)
        {
            if (!shutdown)
            {
                WaitingCallback waiting = new WaitingCallback(callback, state);

                countdownThreads.AddCount(1);
                _waitingCallbacks.Enqueue(waiting);
                _workerThreadNeeded.AddOne();
            }
        }
Exemplo n.º 9
0
        /// <summary>Queues a user work item to the thread pool.</summary>
        /// <param name="callback">
        ///     A WaitCallback representing the delegate to invoke when the thread in the
        ///     thread pool picks up the work item.
        /// </param>
        /// <param name="state">
        ///     The object that is passed to the delegate when serviced from the thread pool.
        /// </param>
        public static void QueueUserWorkItem(WaitCallback callback, object state)
        {
            // Create a waiting callback that contains the delegate and its state.
            // At it to the processing queue, and signal that data is waiting.
            var waiting = new WaitingCallback(callback, state);

            lock (PoolLock)
            {
                _waitingCallbacks.Enqueue(waiting);
            }
            _workerThreadNeeded.AddOne();
        }
Exemplo n.º 10
0
        /// <summary>Event raised when there is an exception on a threadpool thread.</summary>
        /// <summary>A thread worker function that processes items from the work queue.</summary>
        private static void ProcessQueuedItems()
        {
            // Process indefinitely
            while (true)
            {
                //判断有无资源操作,如果有则执行【即从队列中取】,如没有则将当前线程置为等待。注意当前信息量中count数量与_waitingCallbacks队列长度相同。
                //通过QueueUserWorkItem的_workerThreadNeeded.AddOne();代码行来告之在当前线程池中处于wait的线程列表中找出一个线程来执行_waitingCallbacks列队中的任务(Dequeue).
                //如果进程池中所有线程均为执行状态时,则系统还运行QueueUserWorkItem()函数则可视其仅将任务放入队列即可,因为此处的while(true)会不断从队列中获取任务执行。
                _workerThreadNeeded.WaitOne();

                // Get the next item in the queue.  If there is nothing there, go to sleep
                // for a while until we're woken up when a callback is waiting.
                WaitingCallback callback = null;

                // Try to get the next callback available.  We need to lock on the
                // queue in order to make our count check and retrieval atomic.
                lock (PoolLock)
                {
                    if (_waitingCallbacks.Count > 0)
                    {
                        try
                        {
                            callback = (WaitingCallback)_waitingCallbacks.Dequeue();
                        }
                        catch
                        {
                            // ignored
                        } // make sure not to fail here
                    }
                }

                if (callback != null)
                {
                    // We now have a callback.  Execute it.  Make sure to accurately
                    // record how many callbacks are currently executing.
                    try
                    {
                        Interlocked.Increment(ref _inUseThreads);
                        callback.Callback(callback.State);
                    }
                    catch
                    {
                        // ignored
                    }
                    finally
                    {
                        Interlocked.Decrement(ref _inUseThreads);
                    }
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>A thread worker function that processes items from the work queue.</summary>
        private static void ProcessQueuedItems()
        {
            // Process indefinitely
            while (!Project.goingDown)
            {
                // Get the next item in the queue.  If there is nothing there, go to sleep
                // for a while until we're woken up when a callback is waiting.
                WaitingCallback callback = null;
                while (callback == null)
                {
                    // Try to get the next callback available.  We need to lock on the
                    // queue in order to make our count check and retrieval atomic.
                    lock (_waitingCallbacks.SyncRoot)
                    {
                        if (_waitingCallbacks.Count > 0)
                        {
                            //try { callback = (WaitingCallback)_waitingCallbacks.Dequeue(); }
                            try { callback = (WaitingCallback)_waitingCallbacks.Pop(); }
                            catch {}                            // make sure not to fail here
                        }
                    }

                    if (Project.goingDown)
                    {
                        return;
                    }
                    // If we can't get one, go to sleep.
                    if (callback == null)
                    {
                        _workerThreadNeeded.WaitOne();
                    }
                }

                // We now have a callback.  Execute it.  Make sure to accurately
                // record how many callbacks are currently executing.
                try
                {
                    Interlocked.Increment(ref _inUseThreads);
                    callback.Callback(callback.State);
                }
                catch
                {
                    // Make sure we don't throw here.  Errors are not our problem.
                }
                finally
                {
                    Interlocked.Decrement(ref _inUseThreads);
                }
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Event raised when there is an exception on a threadpool thread.
        /// A thread worker function that processes items from the work queue.
        /// </summary>
        private static void ProcessQueuedItems()
        {
            // Process indefinitely
            while (true)
            {
                _workerThreadNeeded.WaitOne();

                // Get the next item in the queue.  If there is nothing there, go to sleep
                // for a while until we're woken up when a callback is waiting.
                WaitingCallback callback = null;

                // Try to get the next callback available.  We need to lock on the
                // queue in order to make our count check and retrieval atomic.
                lock (_poolLock)
                {
                    if (_waitingCallbacks.Count > 0)
                    {
                        try
                        {
                            callback = (WaitingCallback)_waitingCallbacks.Dequeue();
                        }
                        catch (Exception e)
                        {
                            logger.Error("callback回调异常", e);
                        } // make sure not to fail here
                    }
                }

                if (callback != null)
                {
                    // We now have a callback.  Execute it.  Make sure to accurately
                    // record how many callbacks are currently executing.
                    try
                    {
                        Interlocked.Increment(ref _inUseThreads);
                        callback.Callback(callback.State);
                    }
                    catch (Exception e)
                    {
                        logger.Error("callback回调异常", e);
                    }
                    finally
                    {
                        Interlocked.Decrement(ref _inUseThreads);
                    }
                }
            }
        }
Exemplo n.º 13
0
        private void ProcessQueuedItems(object threadIndexWithinPool)
        {
            // Set the ThreadStatic variable indicating the which thread pool we belong to
            ThisThreadGlobalThreadPoolIndex = (int)GlobalThreadPoolIndex;

            if (RoundRobinAffinitize && GlobalThreadPoolIndex != -1)
            {
                int processorID = roundRobinAffinitizeProcessorID;
                if (this.incrementProcessorIDEachThread)
                {
                    processorID += (int)threadIndexWithinPool;
                }
                throw new NotImplementedException("Internal error: AffinitizeThreadRoundRobin needs remediation");
                //Native32.AffinitizeThreadRoundRobin((uint)processorID);
            }

            while (!shutdown)
            {
                WaitingCallback callback    = default;
                bool            gotCallback = false;
                while (!gotCallback)
                {
                    gotCallback = _waitingCallbacks.TryDequeue(out callback);

                    if (!gotCallback)
                    {
                        _workerThreadNeeded.WaitOne();
                    }
                }

                try
                {
                    callback.Callback(callback.State);
                }
                catch (Exception exc)
                {
                    throw new Exception("Exception in ThreadPoolManaged worker item " + exc.ToString());
                }
                finally
                {
                    countdownThreads.Signal(1);
                }
            }
        }
Exemplo n.º 14
0
        /// <summary>一个线程工作器函数,用于处理工作队列中的项目.</summary>
        private static void ProcessQueuedItems()
        {
            // 无限期地处理
            while (true)
            {
                _workerThreadNeeded.WaitOne();

                //获取队列中的下一个项目。 如果那里什么也没有,那就去睡吧吧
                //一段时间,直到我们在回调等待时醒来。
                WaitingCallback callback = null;

                //尝试获取下一个回调。 我们需要锁定
                //队列以使我们的计数检查和检索原子。
                lock (_poolLock)
                {
                    if (_waitingCallbacks.Count > 0)
                    {
                        try { callback = (WaitingCallback)_waitingCallbacks.Dequeue(); }
                        catch { } //确保不要在这里失败
                    }
                }

                if (callback != null)
                {
                    //我们现在有一个回调。 执行它。 确保准确
                    //记录当前正在执行的回调数。
                    try
                    {
                        Interlocked.Increment(ref _inUseThreads);
                        callback.Callback(callback.State);
                    }

                    catch (Exception)
                    {
                    }

                    finally
                    {
                        Interlocked.Decrement(ref _inUseThreads);
                    }
                }
            }
        }
Exemplo n.º 15
0
        /// <summary>清空任何排队工作项的工作队列。 重置池中的所有线程.</summary>
        public static void Reset()
        {
            lock (_poolLock)
            {
                // 清理所有等待的回调
                try
                {
                    // 尝试处理所有剩余的状态
                    foreach (object obj in _waitingCallbacks)
                    {
                        WaitingCallback callback = (WaitingCallback)obj;
                        if (callback.State is IDisposable)
                        {
                            ((IDisposable)callback.State).Dispose();
                        }
                    }
                }
                catch { }

                // 关闭所有现有线程
                try
                {
                    foreach (Thread thread in _workerThreads)
                    {
                        if (thread != null)
                        {
                            thread.Abort("reset");
                        }
                    }
                }
                catch { }

                // 重新初始化池(创建新线程等)
                Initialize();
            }
        }
Exemplo n.º 16
0
 private static void ProcessQueuedItems()
 {
     while (true)
     {
         _workerThreadNeeded.WaitOne();
         WaitingCallback waitingCallback = null;
         lock (_poolLock)
         {
             if (_waitingCallbacks.Count > 0)
             {
                 try
                 {
                     waitingCallback = (WaitingCallback)_waitingCallbacks.Dequeue();
                 }
                 catch
                 {
                 }
             }
         }
         if (waitingCallback != null)
         {
             try
             {
                 Interlocked.Increment(ref _inUseThreads);
                 waitingCallback.Callback(waitingCallback.State);
             }
             catch
             {
             }
             finally
             {
                 Interlocked.Decrement(ref _inUseThreads);
             }
         }
     }
 }
Exemplo n.º 17
0
 /// <summary>Queues a user work item to the thread pool.</summary>
 /// <param name="callback">
 /// A WaitCallback representing the delegate to invoke when the thread in the 
 /// thread pool picks up the work item.
 /// </param>
 /// <param name="state">
 /// The object that is passed to the delegate when serviced from the thread pool.
 /// </param>
 public static void QueueUserWorkItem(WaitCallback callback, object state)
 {
     // Create a waiting callback that contains the delegate and its state.
     // At it to the processing queue, and signal that data is waiting.
     WaitingCallback waiting = new WaitingCallback(callback, state);
     lock (_poolLock) { _waitingCallbacks.Enqueue(waiting); }
     _workerThreadNeeded.AddOne();
 }
Exemplo n.º 18
0
 /// <summary>Queues a user work item to the thread pool.</summary>
 /// <param name="callback">
 /// A WaitCallback representing the delegate to invoke when the thread in the 
 /// thread pool picks up the work item.
 /// </param>
 /// <param name="state">
 /// The object that is passed to the delegate when serviced from the thread pool.
 /// </param>
 public void QueueUserWorkItem(WaitCallback callback, object state)
 {
     try
     {
         // Create a waiting callback that contains the delegate and its state.
         // Add it to the processing queue, and signal that data is waiting.
         WaitingCallback waiting = new WaitingCallback(callback, state);
         lock (_waitingCallbacks.SyncRoot) { _waitingCallbacks.Enqueue(waiting); }
         _workerThreadNeeded.AddOne();
     }
     catch (System.Exception ex)
     {
         Log.Warn("Not expectant exception : ", ex);
     }
 }