Say I have a set of tasks:
var task1 = DoThisAsync(...);
var task2 = DoThatAsync(...);
var task3 = DoOtherAsync(...);
var taskN...
I am looking for a way to process a set of tasks in order (determined by place in containing collection say), but to have the tasks only run/start when its their turn and not before - and have all of that wrapped up in its own task.
Problem constraints / details are:
- These tasks need to be performed in a certain order i.e.task1, task2,...
- The previous task must complete asynchronously before the next can start
- The number of tasks is variable
- A different set of tasks may be nominated each time code is run
- The order and number of tasks is known in advance.
The main problem is that as soon as I call the relevant method (like DoThis()...) to return each task, that task is already 'hot' or running, violating (2) above.
I have tried working with.ContinueWith(..) , but if I call each of tasks like above to set the continuations or add them to a list or collection they've already started.
Not sure if Lazy < T > might help but can't see how at present?
Hope this makes sense as I'm fairly new to async / await / tasks.
Many thanks in advance.
you can simply create tasks with its constructor and then, call execution with .Start() methods.
Here an example:
Calling a method runs code. If you want an object that will call this method later, then use a delegate.
In this case, you could use
Func<Task>
, which is an asynchronous delegate. A list of these should suffice: