c# - Preserve exception when continuing Task<T> -


i've got task<t>:

task<a> sometask = ... 

this task can result in being successful, faulted or cancelled.

i want transform result when task successful, , preserve outcome if not.

this seems difficult when sometask throws exception.

what i've tried:

task<b> resulttask = startmytask().continuewith<b>(     t => foo(t.result),     taskcontinuationoptions.onlyonrantocompletion); 

this results in resulttask being cancelled if sometask faults. want fault.

task<b> resulttask = startmytask().continuewith<b>(     t => foo(t.result)); 

this breaks visual studio debugger because .result throws exception. if press f5, resulttask faults expected, smells.

is there way let resulttask have same outcome sometask if sometask faults?


essentially i'm trying express tasks:

int f() {     throw new someexception(); }  string g(int x) {     return x.tostring(); }  try {     string result = g(f()); } catch (someexception e) {     ... } 

task continuations independent. can used build want, they're not designed scenario.

the first question ask is: can relationship viewed parent/child relationship (e.g., foo parent of startmytask)? if makes sense, may able take advantage of state propogation child parent.

if treating foo "parent" , startmytask "child" doesn't work design-wise, there few other options. continuations bit low-level need (remember, they're "run task when other task completes").

it sounds may trying more "pipeline". currently, rx more suitable kind of thing.

task-based pipelines aren't here yet. parallelextensionsextras library has task-based pipeline class, , async ctp has tpl dataflow library, both of these under-developed @ present. (e.g., pipeline insists on running each stage of pipeline in separate thread, , dataflow has no mechanism automatically propogating exceptions or completion).

so, if can't use rx, write own "pipelinetransform" extension method task , use explicit tcs handle 3 completion situations correctly.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -