I'm building my own queue based system but so far I have a small problem: How can I add an async Task to another function to process it?
My processor function works like this:
I have a Queue object:
public class Queue { public string Id; public CancellationTokenSource Cts; public Task Task; public Queue(Task task, string id, CancellationTokenSource cts) { Id = id; Cts = cts; Task = task; }}
And a command to add my tasks to 2 Lists: List<Queue> ActiveQueues and List<Queue> Queued
The ActiveQueues is a list with all the active queues, and Queued is a list with all queues.
My Add function looks like this:
public async Task Add(Task task, string id, CancellationTokenSource cts)
{
var queue = new Queue(task, id, cts);
if (ActiveQueues.Count < MainWindow.appConfig.MaxAsyncSessions)
{
var exists = ActiveQueues.Find(q => q.Id == queue.Id);
if (exists == null) // I don't want 2 queues with the same id to run at once so I add it to the queued list that's processed by another function.
{
ActiveQueues.Add(queue);
ActiveQueueIds.Add(queue.Id);
await queue.Task;
}
else
{
Queued.Add(queue);
}
}
else
{
Queued.Add(queue);
}
}
The problem is that my task also has parameters so using it like queue.Add(AsyncFunction(param1, param2), "idExample", new CancellationTokenSource()) triggers the function before adding it to the ActiveQueues list.
What would be the correct way to do what I want to do right now?
Taskbefore adding it to the queue?IEnumerable.Any(), which returns aboolinstead of null or an object.Queueclass in BCL, so giving the same name to a custom class with completely different semantics can create confusion.