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!