What would be the best approach for implementing Tasks with a KEY that operate as follows:-
Option 1) Only one of this key is ever pending. Can be used, for example, from ASP.NET MVC to queue up a single render for a thumbnail image no matter how many times the image Url is hit. Only one runs, all other requests wait for that one to complete.
Option 2) All items with the same key must execute sequentially. Can be used for example to ensure that operations that fetch a file from a backing store to a local cache don't all try to get the file to the cache at the same time. Option 1 is a special case of this where subsequent actions with the same key are simply discarded (typically saves just a file-exists-check).
I have an existing WorkQueue that handles both of these cases (as well as Apartment state, ThreadPriority settings and maximum degrees of parallelism). TPL appears to be the best solution for replacing this and will bring improved cancellation options.
Nested Tasks with continuations look hopeful but maintaining a dictionary of currently queue'd tasks soon gets messy between the TaskFactory and the TaskScheduler classes. Inheriting from Task is problematic too since neither TaskFactory nor TaskScheduler are generic on Task.
Most Task Parallel examples assume that the set of tasks is known ahead of time. In this case new tasks are added all the time and need to be either discarded or chained onto existing tasks depending on the operation requested and the key passed in.
Has anyone implemented anything similar to this using TPL and if so, what approach did you take in your Task, TaskScheduler and TaskFactory classes?