Пример #1
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);
			}
		}
Пример #2
0
 public SchedulingService()
 {
     _paused = false;
     _halted = false;
     _threadPool = new ThreadPoolEx(ThreadPoolEx.DefaultIdleTimeout, 10);
     _thread = new Thread(new ThreadStart(Run));
     _thread.Start();
 }
Пример #3
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);
                }
            }
        }