How can something like a ThreadStatic
be used in a TPL Task? My understanding ("Wrox Professional Parallel Programming with C#", p74) is that a Task can switch from one thread to another during execution.
What I want to do?
I want to maintain a session id inside a static class so I don't need to pass this id to all of my methods. My library has methods like login(id)
, logout(id)
and many methods which operate on credentials associated with this id. But I don't want to pass this id to every method. I can make sure my library is called within different thread for different sessions. So saving the id inside login()
in a ThreadStatic
variable will work.
Now I want to use TPL Tasks which are created for me by a ThreadPool
. I can pass my session id to the Task, but if I store this id inside a ThreadStatic
variable it will not survive if my Task switches threads.