c# - Using Task.Unwrap to get to the inner task -


i'm trying access inner task task.unwrap , i'm getting error:

 system.invalidcastexception: unable cast object of type  'system.threading.tasks.unwrappromise`1[system.threading.tasks.taskextensions+voidresult]'  type 'system.threading.tasks.task`1[system.boolean]'. 

to reproduce problem:

    static void main(string[] args)     {         var tcs = new taskcompletionsource<bool>();         tcs.setresult(true);         task task1 = tcs.task;          task<task> task2 = task1.continuewith(             (t) => t, taskcontinuationoptions.executesynchronously);          task task3 = task2.unwrap();          try         {             task<bool> task4 = (task<bool>)task3;              console.writeline(task4.result.tostring());         }         catch (exception e)         {             console.writeline(e.tostring());         }         console.readline();     } 

in real project, i'm provided list of task<task>, each inner task generic task. can not use unwrap access inner tasks , results?

you can using continuewith(), cast inner task task<yourtype>, followed unwrap():

task<bool> task4 = task2.continuewith(t => (task<bool>)t.result).unwrap(); 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -