c# - What is the type VoidTaskResult as it relates to async methods? -


i've been using async (and .net 4.5 really) first time recently, , i've come across has me stumped. there isn't information voidtaskresult class can find on net came here see if has ideas going on.

my code following. obviously, simplified. basic idea call plugin methods, asyncronous. if return task, there no return value async call. if return task<>, there is. don't know in advance type are, idea @ type of result using reflection (isgenerictype true if type type<>) , value using dynamic type.

in real code, calling plugin method via reflection. don't think should make difference behaviour seeing.

// plugin method public task yada() {  // stuff }  public async void doyada() {   task task = yada();   await task;    if (task.gettype().isgenerictype)   {     dynamic dyntask = task;     object result = dyntask.result;     // result   } } 

this works plugin method shown above. isgenerictype false (as expected).

however if change declaration of plugin method ever slightly, isgenerictype returns true , stuff breaks:

public async task yada() {  // stuff } 

when this, following exception thrown on line (object result = dyntask.result;):

runtimebinderexception

if dig task object, appears type. voidtaskresult private type in threading name space nothing in it.

voidtaskresult task

i tried changing calling code:

public async void doyada() {   task task = yada();   await task;    if (task.gettype().isgenerictype)   {     object result = task.gettype().getproperty("result").getmethod.invoke(task, new object[] { });     // result   } } 

this "succeeds" in sense no longer throws, result of type "voidtaskresult" cannot sensibly with.

i should add i'm having hard time formulating real question this. maybe real question "what voidtaskresult?", or "why weird thing happen when call async method dynamically?" or possibly "how call plugin methods optionally asyncronous?" in case, putting out there in hope 1 of gurus able shed light.

this due way class hierarchy around tasks (and particularly task completion sources) designed.

first off, task<t> derives task. assume you're familiar that.

furthermore, can create types of task or task<t> tasks execute code. e.g., if first example returning task.run or whatnot, returning actual task object.

the problem comes in when consider how taskcompletionsource<t> interacts task hierarchy. taskcompletionsource<t> used create tasks don't execute code, rather act notification operation has completed. e.g., timeouts, i/o wrappers, or async methods.

there no non-generic taskcompletionsource type, if want have notification without return value (e.g., timeouts or async task methods), have create taskcompletionsource<t> t , return task<t>. async team had choose t async task methods, created type voidtaskresult.

normally not problem. since task<t> derives task, value converted task , happy (in static world). however, every task created taskcompletionsource<t> of type task<t>, not task, , see reflection/dynamic code.

the end result have treat task<voidtaskresult> task. however, voidtaskresult implementation detail; may change in future.

so, recommend base logic on (declared) return type of yada, not (actual) return value. more closely mimics compiler does.

task task = (task)yadamethod.invoke(...); await task;  if (yadamethod.returntype.isgenerictype) {   ... } 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -