I have a request that contains a batch of "tasks" that need to be completed. An "asyncstate" object that gets passed around via both the request/response contains a collection of "PendingTasks" and "CompletedTasks". Once a task has been complete by a request handler, it gets moved into a "CompletedTasks" collection. If the original requestor sees that there are still outstanding tasks in the response, it will invoke the next handler for the next pending task. So through serialized calls to each handler, eventually all the tasks will get moved to CompletedTasks. I need to make these handler calls serialized because I need a way to know when all the tasks have been completed (btw, the handler may be local or distributed). Making the call order serialized doesn't mean I don't want to process the actual request asynchronously b/c I don't want the calling thread to wait for it to complete before doing something else.
The problem is now after I asynchronously receive a response, and see that there are PendingTasks still, I don't know which is the "next" delegate to invoke asynchronously to continue processing.
Is there a pattern or some other suggestion how I can achieve this?