c# - Not to execute any further code until the async method is completed its execution -


not execute further code until async method completed execution. please let me know how achieve it.

following sample code :

// parent form code private void btnopenform1_click(object sender, eventargs e) {     form1 form1 = new form1();     var result = form1.showdialog();     if (result == system.windows.forms.dialogresult.ok)     {         //     } }   // child form code private void form1_formclosing(object sender, formclosingeventargs e) {     dialogresult result = messagebox.show(string.format("do want save changes?", "confirmation", messageboxbuttons.yesnocancel, messageboxicon.question);     if (result == system.windows.forms.dialogresult.cancel)     {         e.cancel = true;     }     else     {         if (result == dialogresult.yes)         {             e.cancel = true;             this.dialogresult = system.windows.forms.dialogresult.ok;             // here need wait compulsarily till operation completed, no next statement should executed (neither in parent form)              var issavecompleted = await handlesave().configureawait(false);                if(issavecompleted == true)             {                 // dispose objects             }         }         else  // if no clicked         {             this.dialogresult = system.windows.forms.dialogresult.cancel;             // dispose objects         }     } }  public async task<bool> handlesave() {     await dowork();     //     // code here     //           }  public dowork() {     //     // code i/o operation     // } 

in above code, don't want execute of next statements (not in parent form) until handlesave() method completed execution.

there's xy problem here. trying force asynchronous method run synchronously in order block closing form wrong solution.

instead of fighting window lifetimes, need figure out how work them. 1 solution scenario hide form rather closing it; , close when asynchronous save completes. can enable parent form detect when child form closed using taskcompletionsource<dialogresult>.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -