c# - What's the difference between these three Task Continuations? -
i have these 2 scenarios, don't understand why things happening do:
static void main(string[] args) { console.writeline("***starting t1"); //run 2 tasks sequentially task t = firsttask().continuewith(_ => secondtask(), taskcontinuationoptions.onlyonrantocompletion); //register succeded , faulted continuations t.continuewith(_ => completion(), taskcontinuationoptions.onlyonrantocompletion); t.continuewith(_ => faulted(), taskcontinuationoptions.onlyonfaulted); console.readline(); console.writeline("***starting t2"); task t2 = firsttask().continuewith(_ => faulttask(), taskcontinuationoptions.onlyonrantocompletion); t2.continuewith(_ => completion(), taskcontinuationoptions.onlyonrantocompletion); t2.continuewith(_ => faulted(), taskcontinuationoptions.onlyonfaulted); console.readline(); console.writeline("***starting t3"); task t3 = firsttask().continuewith(ant => ant.continuewith(_ => faulttask(), taskcontinuationoptions.onlyonrantocompletion)); t3.continuewith(_ => completion(), taskcontinuationoptions.onlyonrantocompletion); t3.continuewith(_ => faulted(), taskcontinuationoptions.onlyonfaulted); console.readline(); } private static task firsttask() { return task.run(() => { console.writeline("task 1"); thread.sleep(1000); }); } private static task secondtask() { return task.run(() => { console.writeline("task 2"); thread.sleep(1000); }); } private static task faulttask() { return task.run(() => { console.writeline("throw..."); thread.sleep(1000); throw new argumentexception(); }); } private static void completion() { console.writeline("complete"); } private static void faulted() { console.writeline("faulted"); }
in case 1, things ran expected. however, if sleep()
in firsttask()
removed, there no guarantee tasks run in order.
in case 2, faulted()
handler not run. expect occur since there unhandled exception.
in case 3, exception thrown after complete()
handler run. confused why ordering has occurred.
basically want able chain many tasks want , them run in order after previous 1 finished. once have created chain, show wait form , register continuations onlyonrantocompletion
, onlyoncancelled
, onlyonfaulted
final task (read: after have finished) close form - displaying success or error.
is msdn referring options not available multi task continuations?
your return type on t
, t2
task<task>
not task. t3 task<task<task>>
. desired behavior, should able unwrap tasks give task represents entire operation (read docs more info):
console.writeline("***starting t1"); //run 2 tasks sequentially task<task> t = firsttask().continuewith(_ => secondtask(), taskcontinuationoptions.onlyonrantocompletion); //register succeded , faulted continuations t.unwrap().continuewith(_ => completion(), taskcontinuationoptions.onlyonrantocompletion); t.unwrap().continuewith(_ => faulted(), taskcontinuationoptions.onlyonfaulted); console.readline(); console.writeline("***starting t2"); task<task> t2 = firsttask().continuewith(_ => faulttask(), taskcontinuationoptions.onlyonrantocompletion); t2.unwrap().continuewith(_ => completion(), taskcontinuationoptions.onlyonrantocompletion); t2.unwrap().continuewith(_ => faulted(), taskcontinuationoptions.onlyonfaulted); console.readline();
i recommend looking using async/await pattern possible, makes dealing tasks these easier.
Comments
Post a Comment