Is it possible to call a function asynchronously with the same context as the main function without sending the context in parameters?
For Instance
Method1:
- Do some work…
- Call
Method2asynchronously (with taks or delegate etc…) without sending any context in parameters - Do some other work…
Method2:
- Start with the context of
Method1(without setting a context) - Do some work…
I’m working on a console (C#/.NET) project that is supposed to run on a server.
EDIT : I forgot to say : I’m working with VS 2010 (no Async/Await)
I need this because some personal object work with the context.
Absolutely. The common pattern to do this is to encapsulate everything needed in your context into a single class object. If you only have a single context, and don’t intend to ever have multiple concurrent calls, you don’t have to separate it into its own class. (NOTE: You need to to really be sure there are no edge cases that violate this before taking this shortcut.) But it is much cleaner to do so, and not much additional work.
There are numerous ways to do this, one of them is below.