-1

Not to execute any further code until the async method is completed its execution. Please let me know how to achieve it.

Following is 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)
    {
        // do something
    }
}


// Child form code
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    DialogResult result = MessageBox.Show(string.Format("Do you want to 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 I NEED TO WAIT COMPULSARILY TILL THE OPERATION IS COMPLETED, NO NEXT STATEMENT SHOULD BE EXECUTED (NEITHER IN PARENT FORM)

            var isSaveCompleted = await HandleSave().ConfigureAwait(false);  

            if(isSaveCompleted == true)
            {
                // dispose some objects
            }
        }
        else  // if No is clicked
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            // dispose some objects
        }
    }
}

public async Task<bool> HandleSave()
{
    await doWork();
    //
    // some code here
    //          
}

public doWork()
{
    //
    // some code for I/O operation
    //
}

In the above code, I don't want to execute the any of the next statements (not even in the Parent Form) until the HandleSave() method is completed its execution.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Gaurav
  • 31
  • 7
  • are you asking how to make an async method act like a synchronous method? – Claies Jul 14 '15 at 14:26
  • you would have to make a blocking call. I.e wait for the Result but this would defeat the point on having it async – Ahmed ilyas Jul 14 '15 at 14:26
  • Your code doesn't compile, and makes little sense. Post a [MCVE](http://stackoverflow.com/help/mcve) of your problem. – Yuval Itzchakov Jul 14 '15 at 14:26
  • @Claies yes, I want the method to act like synchronous method. – Gaurav Jul 14 '15 at 14:29
  • 1
    If you want it to be blocking, why would you use an AsyncTask in the first place? Is this some sort of code-glue? What are you trying to do? – runDOSrun Jul 14 '15 at 14:29
  • possible duplicate of [How would I run an async Task method synchronously?](http://stackoverflow.com/questions/5095183/how-would-i-run-an-async-taskt-method-synchronously) Also, I agree with @runDOSrun, if you want it to be a synchronous call, don't define it as an async method. – Claies Jul 14 '15 at 14:31
  • @runDOSrun The method with async Task is a common method used in different scenarios. Sometimes, I need it to act it like synchronous method. – Gaurav Jul 14 '15 at 14:34
  • That's not really addressing the point we were making. Just go through the duplicate question and the comments. – runDOSrun Jul 14 '15 at 14:38

1 Answers1

1

There's an XY problem here. Trying to force an asynchronous method to run synchronously in order to block closing the form is the wrong solution.

Instead of fighting window lifetimes, you need to figure out how to work with them. One solution for this scenario is to hide the form rather than closing it; and then actually close it when the asynchronous save completes. You can enable your parent form to detect when the child form is actually closed by using a TaskCompletionSource<DialogResult>.

Community
  • 1
  • 1
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810