c# - Using await inside a ContinueWith() block -


i have following code:

var result = messageboxhelper.msgbox     .showasync("press yes proceed", messageboxbutton.yesno)     .continuewith((answer) =>     {         if (answer.result == messageboxresult.yes)         {             task<bool> asynctask = executeasyncfunc();             //asynctask.unwrap(); ??             asynctask.continuewith((a) =>             {                 // more              }, taskcontinuationoptions.onlyonrantocompletion);         }     }, taskcontinuationoptions.onlyonrantocompletion); } 

invoked elsewhere this:

public async task<bool> executeasyncfunc() {     await callanotherasyncfunction(); } 

i believe need call unwrap(), have tried to, because calling await inside continuewith() block. however, following error when uncomment it:

error cs1929 'task' not contain definition 'unwrap' , best extension method overload 'taskextensions.unwrap(task)' requires receiver of type 'task'

do need use unwrap in context, , if so, doing wrong?

the short answer use await instead of continuewith:

var result = messageboxhelper.msgbox.showasync("press yes proceed", messageboxbutton.yesno); if (answer == messageboxresult.yes) {   var = await executeasyncfunc();   // more  } 

the long answer continuewith has dangerous default options (including using ambient taskscheduler), , await automatically correctly you. go more detail on blog.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -