Async 02
Async 02
Dr M AOUDE ULFG
1
2 // The await operator suspends AccessTheWebAsync .
3 // - AccessTheWebAsync can ’ t continue until
getStringTask is complete .
4 // - Meanwhile , control returns to the caller of
AccessTheWebAsync .
5 // - Control resumes here when getStringTask is
complete .
6 // - The await operator then retrieves the string
result from getStringTask .
7 string urlContents = await getStringTask ;
8 // If AccessTheWebAsync doesn ’ t have any work that it
can do // between calling GetStringAsync and awaiting
its completion , you // can simplify your code by calling
and awaiting in the following // single statement .
9 string urlContents = await client . GetStringAsync () ;
10 // The return statement specifies an integer result .
11 // Any methods that are awaiting AccessTheWebAsync
retrieve the length value .
12 return urlContents . Length ;
13 Dr M}AOUDE ULFG Async and Await in C# Part 2 March 11, 2024 8 / 26
Characteristics of Async Methods
What makes an async method?
The method signature includes an async modifier.
By convention, the name of an async method ends with an ”Async”
suffix.
The return type is one of the following types:
1 Task<TResult >
2 Task
3 void
The method includes at least one await expression, such as:
string urlContents = await getStringTask;
This marks a point where the method can’t continue until the awaited
asynchronous operation is complete. In the meantime, the method is
suspended, and control returns to the method’s caller.
Dr M AOUDE ULFG Async and Await in C# Part 2 March 11, 2024 9 / 26
Async Method Return Types
When defining an async method, the return type should be chosen based
on the scenario. Here are the valid return types for an async method:
Task¡TResult¿: Use this return type if your method has a return
statement where the operand has type TResult.
Task: If your method has no return statement or has a return
statement with no operand, you should use this return type.
void: If you’re writing an async event handler, the return type should
be void.
Any other type with a GetAwaiter method: In some cases, you
may define an async method with a custom return type that
implements a GetAwaiter method.
Say you’re writing a mobile game where pressing a button can inflict
damage on many enemies on the screen. Performing the damage
calculation can be expensive, and doing it on the UI thread would make
the game appear to pause as the calculation is performed!
The best way to handle this is to start a background thread which does
the work using Task.Run, and await its result. This will allow the UI to
feel smooth as the work is being done.
To send data to the server using the POST method, you can use the
PostAsync method.
1 string apiUrl = " https :// api . example . com / data " ;
2 var data = new StringContent ( " {\" name \":\" John \" ,\" age
\":30} " ,
3 Encoding . UTF8 , " application / json " ) ;
4
5 HttpRes p o ns e M es s age response = await httpClient .
PostAsync ( apiUrl , data ) ;
6 if ( response . Is S uccessStatusCode )
7 {
8 string responseContent = await response . Content .
ReadAsStri ngAsyn c () ;
9 // Process the response data here
10 }
11
The .NET Framework saves the location in your method where the
asynchronous thread starts to be able to resume back to the right
step in the execution.
The .NET Framework saves the context of the current thread which
includes:
1 Execution Context.
2 Security Context.
3 Call Context.