Click here to Skip to main content
15,892,809 members

TaskQueue. How is this bad?

essence asked:

Open original thread
I've reviewed the QueuedTaskScheduler and I'm very confused.
http://blogs.msdn.com/b/pfxteam/archive/2010/04/09/9990424.aspx[^]

So I wrote this class to do what I wanted.
What I wanted is a class that will queue up tasks in the background and start them in order with a lower priority. Also managing a maximum concurrency.

But also I wanted to allow for the the provided Action/Task to be executed synchronously (or not) when a maximum queue limit is reached.

Can someone tell me how this might be bad or why I should simply use a TaskScheduler?

C#
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Utilities
{

	/// <summary>
	/// Acts as a producer-consumer for tasks...
	/// </summary>
	public class TaskQueue
	{
		readonly object _syncRoot = new Object();
		readonly Queue<Action> _actions = new Queue<Action>();
		readonly List<Thread> _tasks = new List<Thread>();

		int _maxConcurrency;
		public int MaxConcurrency
		{
			get
			{
				return _maxConcurrency;
			}
			set
			{
				Contract.Requires(value > 0);
				lock (_tasks)
					_maxConcurrency = value;
			}
		}

		int _maxTasks;
		public int MaxTasks
		{
			get
			{
				return _maxTasks;
			}
			set
			{
				Contract.Requires(value > 0);
				lock (_tasks)
					_maxTasks = value;
			}
		}

		public int Count {
			get {
				return _tasks.Count + _actions.Count;
			}
		}
		// Default limits to 4.
		public TaskQueue(int maxConcurrency = 4, int maxTasks = 24)
		{
			MaxConcurrency = maxConcurrency;
			MaxTasks = Math.Max(maxConcurrency,maxTasks);
		}

		void Cycle()
		{
			if (_tasks.Count < _maxConcurrency)
			{
				lock (_syncRoot)
				{
					if (_tasks.Count < _maxConcurrency && _actions.Any())
					{
						var task = new Thread(new ThreadStart(_actions.Dequeue()));
						_tasks.Add(task);
						task.IsBackground = true;
						task.Priority = ThreadPriority.Lowest;

						Task.Factory.StartNew(() =>
						{
							task.Start();
							task.Join();
							lock (_syncRoot)
								_tasks.Remove(task);
							Cycle();
						});
					}
				}
			}
		}

		public bool Queue(Action action, bool executeSynchronousIfLimitreached = true)
		{
			if(Count < MaxTasks) {
				lock (_syncRoot)
				{
					if (Count < MaxTasks)
					{
						_actions.Enqueue(action);
						Cycle();
						return true;
					}
				}
			}

			Debug.Print("Task limit reached: " + MaxTasks);
				
			if(executeSynchronousIfLimitreached)
				action();

			return false;
		}

	}
}
Tags: Threads, TPL, Priority, TaskScheduler

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900