The error is that your main function does not await for completion of procedure IncluiValores. Your main program finishes before procedure IncluiValores is finished.
Because of this error I assume you still have some trouble understanding what happens when you use async-await.
Someone here on StackOverflow (alas I can't find it anymore), explained it to me using the following metaphor.
ADDITION: I found the metaphore
It is in this interview with Eric Lippert
Search somewhere in the middle for async-await
End Adition
Suppose you need to make breakfast. You want to toast some bread, boil some eggs and make some tea.
Synchronous
- Put bread in the toaster and wait until the bread is toasted
- Remove the bread from the toaster.
- Start boiling water, wait until the water boils
- Put some eggs in the boiling water and wait 7 minutes until your eggs are ready
- Remove the eggs from the water
- Start boiling water for your tea and wait until the water boils
- When the water boils you put it in the teapot and add some tea leaves and wait 4 minutes
- Finally you get everything together and bring it to your breakfast table.
You see you do a lot of waiting which is a waste of time, not to mention that your bread is probably cold by the time the tea is finished.
It would be much more efficient if you didn't wait all the time, but would start things simultaneously
using async-await: asynchronous using one thread
- Start as in the synchronous case: Put bread in the toaster
- but now you don't wait until the bread is toasted. Remember what you should do when the bread is toasted (remember this as Task A)
- Start boiling water, but do not wait for the water to boil. Remember what you should do when the water boils (remember this as Task B)
Start boiling water for your tea, but do not wait for the waiter to boil. Remember what you should do when the tea kettle boils (remember this as Task C)
Wait until any of the Tasks A / B / C is finished. Continue what you remembered what you should do when the task was finished. If this needs some other waiting (time for the eggs or the tea to be ready), do not wait for it, but remember it as Tasks D and E and start waiting for all not finished tasks.
Note that in this method there is still only one person doing all the stuff. If you use async-await this way, there is only one thread involved. This thread is only waiting if it really has nothing to do. The advantage of this is that you don't face the problems you normally encounter when using several threads.
Asynchronous using several threads
You could hire several cooks: one to toast bread and one to boil eggs while you make the tead. This is an expensive method: start several threads, while the threads are doing nothing but wait most of the time. You also have the problems that the three cooks have to synchronize to make sure that they don't use the one-fire stove at the same time.
Stephen Cleary wrote a comprehensive article that describes this async-await behaviour in Async and Await (Thank you Stephen!)
static void Main(string[] args)
{
var breakFast = await Task.Run( () => MakeBreakFast());
// once here I know breakfast is ready
Eat(breakFast);
}
private static async Task<BreakFast> MakeBreakFast()
{
var taskToastBread = ToastBreadAsync();
// do not await. As soon as the procedure awaits come back to do the next statement:
var taskBoilEggs = BoilEggsAsync();
// again do not await. Come back as the procedure awaits
var taskMakeTea = MakeTeaAsync();
// do not wait, but come bask as soon as the procedure await
// now wait until all three tasks are finished:
await Task.WhenAll (new Task[] {taskToasBread, taskBoilEggs, taskMakeTea});
// if here: all tasks are finished. Property Result contains the return value of the Task:
return new BreakFast()
{
Toast = taskToastBread.Result,
Eggs = taskBoilEggs.Result,
Tea = taksMakeTea.Result,
}
}
private static Task<Toast> ToastBreadAsync()
{
var sliceOfBread = Loaf.CutSliceOfBread();
Toaster.Insert(sliceOfBread);
await Toaster.Toast();
// the function does not wait but return to the caller.
// the next is done when the caller await and the toaster is ready toasting
var toast = Toaster.Remove();
return Toast();
}
private static Task<Eggs> BoilEggsAsync()
{
var eggPan = ...
await eggPan.BoilWater();
var eggs = Fridge.ExtreactEggs();
EggPan.Insert(eggs);
await Task.Delay(TimeSpan.FromMinutes(7));
return EggPan.Remove();
}
You'll probably know by now how to make tea.