Any time that you need to do an action on a remote server, your program generates the request, sends it, then waits for a response. I will use SaveChanges()
and SaveChangesAsync()
as an example but the same applies to Find()
and FindAsync()
.
Say you have a list myList
of 100+ items that you need to add to your database. To insert that, your function would look something like so:
using(var context = new MyEDM())
{
context.MyTable.AddRange(myList);
context.SaveChanges();
}
First you create an instance of MyEDM
, add the list myList
to the table MyTable
, then call SaveChanges()
to persist the changes to the database. It works how you want, the records get committed, but your program cannot do anything else until the commit finishes. This can take a long time depending on what you are committing. If you are committing changes to the records, entity has to commit those one at a time (I once had a save take 2 minutes for updates)!
To solve this problem, you could do one of two things. The first is you can start up a new thread to handle the insert. While this will free up the calling thread to continue executing, you created a new thread that is just going to sit there and wait. There is no need for that overhead, and this is what the async await
pattern solves.
For I/O opperations, await
quickly becomes your best friend. Taking the code section from above, we can modify it to be:
using(var context = new MyEDM())
{
Console.WriteLine("Save Starting");
context.MyTable.AddRange(myList);
await context.SaveChangesAsync();
Console.WriteLine("Save Complete");
}
It is a very small change, but there are profound effects on the efficiency and performance of your code. So what happens? The begining of the code is the same, you create an instance of MyEDM
and add your myList
to MyTable
. But when you call await context.SaveChangesAsync()
, the execution of code returns to the calling function! So while you are waiting for all those records to commit, your code can continue to execute. Say the function that contained the above code had the signature of public async Task SaveRecords(List<MyTable> saveList)
, the calling function could look like this:
public async Task MyCallingFunction()
{
Console.WriteLine("Function Starting");
Task saveTask = SaveRecords(GenerateNewRecords());
for(int i = 0; i < 1000; i++){
Console.WriteLine("Continuing to execute!");
}
await saveTask;
Console.Log("Function Complete");
}
Why you would have a function like this, I don't know, but what it outputs shows how async await
works. First let's go over what happens.
Execution enters MyCallingFunction
, Function Starting
then Save Starting
gets written to the console, then the function SaveChangesAsync()
gets called. At this point, execution returns to MyCallingFunction
and enters the for loop writing 'Continuing to Execute' up to 1000 times. When SaveChangesAsync()
finishes, execution returns to the SaveRecords
function, writing Save Complete
to the console. Once everything in SaveRecords
completes, execution will continue in MyCallingFunction
right were it was when SaveChangesAsync()
finished. Confused? Here is an example output:
Function Starting
Save Starting
Continuing to execute!
Continuing to execute!
Continuing to execute!
Continuing to execute!
Continuing to execute!
....
Continuing to execute!
Save Complete!
Continuing to execute!
Continuing to execute!
Continuing to execute!
....
Continuing to execute!
Function Complete!
Or maybe:
Function Starting
Save Starting
Continuing to execute!
Continuing to execute!
Save Complete!
Continuing to execute!
Continuing to execute!
Continuing to execute!
....
Continuing to execute!
Function Complete!
That is the beauty of async await
, your code can continue to run while you are waiting for something to finish. In reality, you would have a function more like this as your calling function:
public async Task MyCallingFunction()
{
List<Task> myTasks = new List<Task>();
myTasks.Add(SaveRecords(GenerateNewRecords()));
myTasks.Add(SaveRecords2(GenerateNewRecords2()));
myTasks.Add(SaveRecords3(GenerateNewRecords3()));
myTasks.Add(SaveRecords4(GenerateNewRecords4()));
await Task.WhenAll(myTasks.ToArray());
}
Here, you have four different save record functions going at the same time. MyCallingFunction
will complete a lot faster using async await
than if the individual SaveRecords
functions were called in series.
The one thing that I have not touched on yet is the await
keyword. What this does is stop the current function from executing until whatever Task
you are awaiting completes. So in the case of the original MyCallingFunction
, the line Function Complete
will not be written to the console until the SaveRecords
function finishes.
Long story short, if you have an option to use async await
, you should as it will greatly increase the performance of your application.