NOTE. I'm not saying this is a good idea, just trying to figure out if there is a βbetterβ option than this brute force.
This appeared in a previous SO @ thread. How do I get the current job link?
However, this thread was a bit more limited by a specific interface.
The brute force approach I quickly got together with just uses a dictionary of weak links.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace GetCurrentTaskExample { public static class TaskContext {
However, this clearly means that everything that creates the tasks is forced to deal with this additional headache, therefore it is not very useful, especially for the WRT C # 5 asynchronous methods, where the task is not so explicitly created.
Again, it might be a bad idea to have code that needs it, so consider it more like a mental exercise. :)
source share