Reputation: 91
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
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