Here's how it works, I want to call an operation asynchronously. If the operation is already declared Async it's simple:
- Add async to your function that is calling the existing async function
- Change the return value to Task (for void) or Task (for any other type T)
- Place the keyword await in front of the call to that async function. And follow that call with the rest of my function's logic.
Here's an example (taken from MSDN Docs on Async Await).
// Three things to note in the signature:
// - The method has an async modifier.
// - The return type is Task or Task<T>. (See "Return Types" section.)
// Here, it is Task<int> because the return statement returns an integer.
// - The method name ends in "Async."
async Task<int> AccessTheWebAsync()
// You need to add a reference to System.Net.Http to declare client.
HttpClient client = new HttpClient();
// GetStringAsync returns a Task<string>. That means that when you await the
// task you'll get a string (urlContents).
// The await operator suspends AccessTheWebAsync.
// - AccessTheWebAsync can't continue until getStringTask is complete.
// - Meanwhile, control returns to the caller of AccessTheWebAsync.
// - Control resumes here when getStringTask is complete.
// - The await operator then retrieves the string result from getStringTask.
string urlContents = await client.GetStringAsync("http://www.microsoft.com");
// The return statement specifies an integer result.
// Any methods that are awaiting AccessTheWebAsync retrieve the length value.
Reading the linked article will describe what's happening in the background.
You can also explicitly perform continuations in C# using the ContinueWith operation