I am looking for the ideal/thread safe implementation of a task queue that will start each task based on an internal condition (which i also only want to be checked at a timed interval), but also check that there aren't too many other tasks running. I also want a way to return progress of each task in the queue.
To give some background, I have a FileSystemWatcher that sends an event on a new folder being made. I then want to put this into a queue to process the folder, when it is no longer locked. But at the same time I want to guard against too many instances of the Process task from running.
Heres some pseudo-code of what I am after.
private static void EventToQueue(object sender, EventArgs e)
{
ProcessQueue.Enqueue(Task, condition);
}
private static async void NewQueueObject(object sender, QueueObject e)
{
if (e.Condition && ProcessQueue.ActiveTask < 4)
{
var progress = new Progress<int>();
progress.ProgressChanged += ( s, e ) =>
{
UpdateProgress(e.Value);
};
await LongProcess(e, progress);
}
else
{
e.Delay(30);
}
}