Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make sure a task is done before returning from a function?

I have a decent understanding of how Async/Await works in C#.

I understand that when the await keyword is reached, control is given back to the calling function and that is how asynchronous execution is achieved (if I am wrong here, I would appreciate correction and a better explanation).

However I am not sure as to how I would make sure that an async function has finished executing before returning from a function.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Take the example below:

private static async Task<bool> LoadDataIntoLocalAppFile(ZipArchiveEntry entry, string key)
{
    try
    {
        /* SqLiteAsyncConnection is an instance variable in the class that has this function.*/
        string pathToUse = "localDatabase.db"
        if (SqLiteAsyncConnection != null)
        {
            await SqLiteAsyncConnection.CloseAsync()
                .ContinueWith(x => entry.ExtractToFile(pathToUse, true));
        }
        else
        {
            entry.ExtractToFile(pathToUse, true);
        }
        return true;
    }
    catch (Exception ex)
    {
        Colsole.WriteLine(ex.ToString());
        return false;
    }
}

In the above code snippet, I want to make sure that my SqLiteAsyncConnection is closed before I replace the data in the .db file with the contents of entry (so that it does not error out). As such I have used ContinueWith (if this is wrong I would appreciate some clarification).

But I also want to ensure that await SqLiteAsyncConnection.CloseAsync().ContinueWith(x => entry.ExtractToFile(pathToUse, true)); finishes its execution before the function returns. That is I want to ensure that this function does not return true inaccurately, and that it does not return true before await SqLiteAsyncConnection.CloseAsync() .ContinueWith(x => entry.ExtractToFile(pathToUse, true)); finishes its execution.

How do I achieve this?

>Solution :

You don’t need the ContinueWith when you have await there. You would use ContinueWith if you don’t use await, it can be seen as a type of callback once the parent task has completed/failed.

await SqLiteAsyncConnection.CloseAsync()
                .ContinueWith(x => entry.ExtractToFile(pathToUse, true));

//can be replaced by

await SqLiteAsyncConnection.CloseAsync();
entry.ExtractToFile(pathToUse, true);

When you hit an await section in your code, your function isn’t done, it doesn’t return true, it gets suspended in a sense and the control is given back to calling thread. Once all your awaits have completed in the function, only then does it return the result.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading