예제 #1
0
 private void ProcessQueuedItems()
 {
     _threadPool = this;
     try
     {
         bool flag = false;
         while (!this._shutdown)
         {
             this._workerThreads[Thread.CurrentThread] = DateTime.Now;
             WorkItem workItem = this.Dequeue();
             this._workerThreads[Thread.CurrentThread] = DateTime.Now;
             if ((workItem == null) && (this._workerThreads.Count > this._threadPoolStartInfo.MinWorkerThreads))
             {
                 lock (this._workerThreads.SyncRoot)
                 {
                     if (this._workerThreads.Count > this._threadPoolStartInfo.MinWorkerThreads)
                     {
                         if (this._workerThreads.Contains(Thread.CurrentThread))
                         {
                             this._workerThreads.Remove(Thread.CurrentThread);
                         }
                         return;
                     }
                 }
             }
             if (workItem != null)
             {
                 int num;
                 try
                 {
                     flag = false;
                     if (!workItem.StartingWorkItem())
                     {
                         continue;
                     }
                     num              = Interlocked.Increment(ref this._inUseWorkerThreads);
                     flag             = true;
                     _currentWorkItem = workItem;
                     this.ExecuteWorkItem(workItem);
                 }
                 catch (Exception)
                 {
                 }
                 finally
                 {
                     if (workItem != null)
                     {
                         workItem.DisposeState();
                     }
                     _currentWorkItem = null;
                     if (flag)
                     {
                         num = Interlocked.Decrement(ref this._inUseWorkerThreads);
                     }
                     this.DecrementWorkItemsCount();
                 }
             }
         }
     }
     catch (ThreadAbortException)
     {
         Thread.ResetAbort();
     }
     catch (Exception)
     {
     }
     finally
     {
         if (this._workerThreads.Contains(Thread.CurrentThread))
         {
             this._workerThreads.Remove(Thread.CurrentThread);
         }
     }
 }
예제 #2
0
		/// <summary>
		/// A worker thread method that processes work items from the work items queue.
		/// </summary>
		private void ProcessQueuedItems()
		{
			// Initialize the _threadPool variable
			_threadPool = this;

			try
			{
				bool inUseWorkerThreadsWasIncremented = false;

				// Process until shutdown.
				while(!_shutdown)
				{
					// Update the last time this thread was alive.
					_workerThreads[Thread.CurrentThread] = DateTime.Now;
					// Wait for a work item, shutdown, or timeout
					WorkItem workItem = Dequeue();
					// Update the last time this thread was alive.
					_workerThreads[Thread.CurrentThread] = DateTime.Now;
					// On timeout or shut down.
					if (workItem == null)
					{
						// Double lock for quit.
						if (_workerThreads.Count > _threadPoolStartInfo.MinWorkerThreads)
						{
							lock(_workerThreads.SyncRoot)
							{
								if (_workerThreads.Count > _threadPoolStartInfo.MinWorkerThreads)
								{
									//Quit
									if (_workerThreads.Contains(Thread.CurrentThread))
									{
										_workerThreads.Remove(Thread.CurrentThread);
									}
									break;
								}
							}
						}
					}
					// If we didn't quit then skip to the next iteration.
					if (workItem == null)
						continue;

					try 
					{
						// Initialize the value to false
						inUseWorkerThreadsWasIncremented = false;

						// Change the state of the work item to 'in progress' if possible.
						// The cancel mechanism doesn't delete items from the queue,  it marks the work item as canceled, 
						// and when the work item is dequeued, we just skip it.
						if (!workItem.StartingWorkItem())
							continue;
						// Execute the callback.  Make sure to accurately record how many callbacks are currently executing.
						int inUseWorkerThreads = Interlocked.Increment(ref _inUseWorkerThreads);
						// Mark that the _inUseWorkerThreads incremented, so in the finally{} statement we will decrement it correctly.
						inUseWorkerThreadsWasIncremented = true;
						// Set the _currentWorkItem to the current work item
						_currentWorkItem = workItem;
						ExecuteWorkItem(workItem);
					}
					catch(Exception)
					{
						// Do nothing
					}
					finally
					{
						if (workItem != null)
							workItem.DisposeState();
						_currentWorkItem = null;
						// Decrement the _inUseWorkerThreads only if we had  incremented it.
						if (inUseWorkerThreadsWasIncremented)
						{
							int inUseWorkerThreads = Interlocked.Decrement(ref _inUseWorkerThreads);
						}
						// Decrement the number of work items here so the idle ManualResetEvent won't fluctuate.
						DecrementWorkItemsCount();
					}
				}
			} 
			catch(ThreadAbortException)
			{
				// Handle the abort exception gracfully.
				Thread.ResetAbort();
			}
			catch(Exception)
			{
			}
			finally
			{
				if (_workerThreads.Contains(Thread.CurrentThread))
					_workerThreads.Remove(Thread.CurrentThread);
			}
		}
 public SchedulingService()
 {
     _paused = false;
     _halted = false;
     _threadPool = new ThreadPoolEx(ThreadPoolEx.DefaultIdleTimeout, 10);
     _thread = new Thread(new ThreadStart(Run));
     _thread.Start();
 }
예제 #4
0
        /// <summary>
        /// A worker thread method that processes work items from the work items queue.
        /// </summary>
        private void ProcessQueuedItems()
        {
            // Initialize the _threadPool variable
            _threadPool = this;

            try
            {
                bool inUseWorkerThreadsWasIncremented = false;

                // Process until shutdown.
                while (!_shutdown)
                {
                    // Update the last time this thread was alive.
                    _workerThreads[Thread.CurrentThread] = DateTime.Now;
                    // Wait for a work item, shutdown, or timeout
                    WorkItem workItem = Dequeue();
                    // Update the last time this thread was alive.
                    _workerThreads[Thread.CurrentThread] = DateTime.Now;
                    // On timeout or shut down.
                    if (workItem == null)
                    {
                        // Double lock for quit.
                        if (_workerThreads.Count > _threadPoolStartInfo.MinWorkerThreads)
                        {
                            lock (_workerThreads.SyncRoot)
                            {
                                if (_workerThreads.Count > _threadPoolStartInfo.MinWorkerThreads)
                                {
                                    //Quit
                                    if (_workerThreads.Contains(Thread.CurrentThread))
                                    {
                                        _workerThreads.Remove(Thread.CurrentThread);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    // If we didn't quit then skip to the next iteration.
                    if (workItem == null)
                    {
                        continue;
                    }

                    try
                    {
                        // Initialize the value to false
                        inUseWorkerThreadsWasIncremented = false;

                        // Change the state of the work item to 'in progress' if possible.
                        // The cancel mechanism doesn't delete items from the queue,  it marks the work item as canceled,
                        // and when the work item is dequeued, we just skip it.
                        if (!workItem.StartingWorkItem())
                        {
                            continue;
                        }
                        // Execute the callback.  Make sure to accurately record how many callbacks are currently executing.
                        int inUseWorkerThreads = Interlocked.Increment(ref _inUseWorkerThreads);
                        // Mark that the _inUseWorkerThreads incremented, so in the finally{} statement we will decrement it correctly.
                        inUseWorkerThreadsWasIncremented = true;
                        // Set the _currentWorkItem to the current work item
                        _currentWorkItem = workItem;
                        ExecuteWorkItem(workItem);
                    }
                    catch (Exception)
                    {
                        // Do nothing
                    }
                    finally
                    {
                        if (workItem != null)
                        {
                            workItem.DisposeState();
                        }
                        _currentWorkItem = null;
                        // Decrement the _inUseWorkerThreads only if we had  incremented it.
                        if (inUseWorkerThreadsWasIncremented)
                        {
                            int inUseWorkerThreads = Interlocked.Decrement(ref _inUseWorkerThreads);
                        }
                        // Decrement the number of work items here so the idle ManualResetEvent won't fluctuate.
                        DecrementWorkItemsCount();
                    }
                }
            }
            catch (ThreadAbortException)
            {
#if !NET5_0_OR_GREATER
                // Handle the abort exception gracfully.
                Thread.ResetAbort();
#endif
            }
            catch (Exception)
            {
            }
            finally
            {
                if (_workerThreads.Contains(Thread.CurrentThread))
                {
                    _workerThreads.Remove(Thread.CurrentThread);
                }
            }
        }