I have ConcurrentQueue TasksCollection
wich contains ITask
objects. It is not Task
class of .Net framework.
public class TaskRunner:IDisposable
{
private Task _orderTask;
public TaskRunner()
{
TasksCollection = new ConcurrentQueue<ITask>();
}
public void Start()
{
_needToStopOrderTask = false;
_orderTask = Task.Factory.StartNew(() => OrderTaskLoop());
}
public void Stop()
{
lock(_lockObject)
_needToStopOrderTask = true;
}
}
So, when some event happens i create ITask
and add to ConcurrentQueue
new task.
And at thread loop i get each task and execute it (execute some code synchronously and consistently. It seems, that i can not concurrent it.
private void OrderTaskLoop()
{
try
{
if (TasksCollection.Count == 0)
return;
while (!_needToStopOrderTask)
{
if(TasksCollection.Count>100)//too many tasks
{
//what should i do here?
}
ITask task = null;
var tryTake = TasksCollection.TryDequeue(out task);
///execute
}
}
}
So, at my situation i think that i can just clear queue and continue work,because my runner work in real-time context. But, may be some pattern of this situation exists?
What should i do if ConcurrentQueue
count is too big?
Thank you!
Actually, as soon as tasks arrive quicker than they can be processed, there is a few options I could suggest:
Ignore some tasks.
a) E.g. stop adding new tasks (like @Mikael Nitell suggests in comment).
b) Or just clean up the queue (like you are suggesting by yourself)
c) Introduce timeouts for tasks. Tasks might be deleted when they are timed out. It is possible to use different timeouts for different types of tasks. If this is suitable for you.
Process tasks quicker to avoid Queue length growing up.
a) Review tasks processing and find the ways to reduce processing time.
b) Find the way how to run tasks processing in parallel. If tasks cannon be paralleled completely, they probably can be partially paralleled.
If the load of events is irregular, then bigger queue length might be allowed.
When events arrive intensively - the tasks are being collected and queue length is increasing.
When events don't arrive or arrive slowly - queue length is decreasing because tasks are processed quicker than they arrive.
The most suitable option depends on the exact situation and on the project needs.
Also the combination of those options can be considered.