❔ ✅ Why should I use Func<Task> in this situation?
I have following class:
logic is simple
I just don't understand following:
if I change Func<Task> to simple Task everythink wouldn't work
8 Replies
because of the loop in
Interval()
, with the factory u basically invoke the reading/writing (up to) multiple times.
if u just pass a task, it would do the reading/writing at max onceI don't understand
I pass a task to
Interval
method that should invoke task in this condition: while (await _timer.WaitForNextTickAsync(_cts.Token))
so.. it should wait wait for tick and invoke methodyeah, because
await f();
, which produces a new Task
instance each time.
if u just pass the task directly, it would look similar to await theTask;
, which means after the first iteration of the loop the task will stay completed
maybe this shows the difference in behavior better
Example1
would wait exactly once for 1 sec, Example2
alwaysI need time to understand this ..
thanks for helping!
in
Example1
exactly 1 task was created, so once await task;
returns, task
is completed, a subsequent await task;
would instantly return, because of that
in Example2
, it always creates a new task, before awaiting it Task.Delay(1000)
is basically the factory method for the task, just like f
in ur codeahhh
I understand
we create new task in while every time in second case
thanks
yep (and with the
Func<Task>
)Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.