[Suggestion] Method local Task variables accessible in runtime #2042
Replies: 3 comments
-
(Please consider editing your post to enable syntax highlighting.) I think what you're asking here is some kind of syntactical sugar to supply a |
Beta Was this translation helpful? Give feedback.
-
I'd need to hear some more use cases for something like this. Writing custom task types seems pretty niche already so a feature targeting specific custom task types would be doubly-so. Also, anything that encourages exposing mutable state in an asynchronous context seems like a big no-no. |
Beta Was this translation helpful? Give feedback.
-
Kinda yes. Thing is if MyNum was actually an AsyncLocal field it would also apply to all functions within Something() so if i called another function they'd have the same MyNum which isn't what i want. I want MyNum to be independent for every MyTask function. For example this is necessary for the use i just edited into the post |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Essentially i'm asking to access variables in the async return type inside of the method.
So it looks something like this with Task.
That use is just to explain the concept its actual use will work with classes, something like so.
This will allow you to interact with the task as its running for example
Itll also allow you to reuse tools on other async functions such as adding a DateTime Started variable and setting it in the constructor as well as better extension methods in a task wrapper.
This pretty much replaces the need for AsyncLocal or ThreadLocal since itll be far easier to manage and i assume better performance if AsyncLocal and ThreadLocal actually have to search for the current thread id while this is just standard class variables no different from any other like IsCompleted. (This could also be used to alternate CancellationToken by just having a cancel variable on tasks)
Here's an example where this is necessary.
This implements a custom delay method with its usefulness being in scripted animations (came up with it for a video game) allowing you to speed up all MyTask animations by having their own speed variable and make them look smoother by catching up on the actual animation's runtime through calculating it from the last time called.
Beta Was this translation helpful? Give feedback.
All reactions