c# - Why does ContinueWith execute if I do a delay inside a task -
i'm little confused continuewith in task parallel library seems do.
my understanding shouldn't called until task has been completed. if i'm in while true loop shouldn't getting called @ all.
datetime t = datetime.now.addseconds(10); task.factory.startnew(async () => { while (true) { if (t < datetime.now) //after 10s throw { throw new exception(); //i expect run continuation here } console.writeline("looped"); await task.delay(new timespan(0, 0, 1)); } } ).continuewith(ct => console.writeline("continued with: {0}",ct.result.status)) ;
i expect following code not run continuewith method until exception thrown isn't case. instead following output:
looped continued with: waitingforactivation looped looped looped looped looped looped looped looped looped
why call continuewith when hit first delay?
you've created evil async void
delegate.
it finishes (completing original task
) synchronous part completes.
there no way observe asynchronous part of original delegate.
your original delegate returns asynchronous task
representing async part of operation.
however, outer task
completes synchronous part finishes, , never wait inner task
finish.
instead, should call task.run()
, accepts func<task>
.
way, asynchronous portions of original delegate contribute original task.
Comments
Post a Comment