A K P
A K P

Reputation: 91

Multiple Awaitable in one Asynch method

I have an async method Save which internally calls CompleteTaskAsync method as below:

           public async void Save()
            {
                await CompleteTaskAsync();

                MessageBox.Show("TaskCompleted");
            }

            private async Task CompleteTaskAsync()
            {
                int someResult = await GetResultAsync();//Does some calculation which take few seconds.

                await FinalSave(someResult);//Note: This method also interact UI internally
            }

CompleteTaskAsync calls two methods in which second method is using output of the first.

In current case MessageBox is coming just after calling first method(GetResultAsync), without calling second method(FinalSave).

I want both await method to be executed before showing the message. Is there any way to execute both await methods before returning to its calling method.

Upvotes: 0

Views: 82

Answers (1)

Timothy Shields
Timothy Shields

Reputation: 79441

MessageBox.Show("TaskCompleted"); will not be called until the Task instance returned by CompleteTaskAsync is completed, which will in turn not be completed until both of the Task instances returned by GetResultAsync and FinalSave are completed.

Your use of async/await and the behavior you are requesting are consistent. Can you give any more information? I believe your problem does not lie in the code shown.

Upvotes: 2

Related Questions