C# await

WBOY
Release: 2024-09-03 15:04:52
Original
522 people have browsed it

The await keyword in C# programming language is used to suspend all async methods enclosed until the point where the operations presented by the asynchronous method are completed. In order or a developer to call multiple functions in an asynchronous way, async and await are highly used and recommended. It is possible to inherit almost all features of asynchronous programming with just simple implementation of await function along with async. Let us now move on to understanding the standard syntax to implement the await keyword.

Syntax:

The standard syntax for await keyword is simple, it includes the keyword followed by the function or method. The syntax is as follows:

await anyFunction();
Copy after login

Here, we have the syntax starting with the await keyword, followed by a function, which could be any function over the program. The freedom is to call and pass await keyword to any method class or functions within the syntax.

How await Keyword Works in C#?

Moving on, we will understand how the await keywords actually works in C#, and basically, the await keyword halts the operations of the async method until the operands complete the operations. Then, after the completion of the asynchronous operation, the await keyword returns to the operation result if there is any. This same await keyword doesn’t block any threat of the async method and after suspending the enclosing async method, the control of the program is returned back to the program caller. So, this is how the await keyword works and how to program flows while having to implement the await keyword. Moving on, we will now implement the await keyword and see how it works in real-life examples.

Examples of C# await

Now that we have understood what the await keyword is and how it works, let us move on to demonstrating the await keyword along with examples. Here for our first example, we will simply use a single method, with a print statement and an await keyword.

Example #1

Code:

using System;
using System.Threading.Tasks;
class Program {
static void Main(string[] args) {
first_meth();
Console.ReadKey();
}
public static async Task first_meth() {
await Task.Run(() =>  {
for (int a = 0 ; a < 2; a++ )  {
Console.WriteLine(" Simple demonstration of await keyword.");
}
});
}
}
Copy after login

Output:

C# await

Code Explanation: This is a very simple demonstration of the await keyword, where we have a single method and the main method. The main method calls for the first method and is bound to be executed. The method has an implementation of the await keyword. The output will be a print statement printed twice.

Example #2

As expected, the output is the statement printed twice. For our next example, we will have two different methods which are in no way dependent and we will call them through the main method. The code for the program is as follows:

Code:

using System;
using System.Threading.Tasks;
class Program {
static void Main(string[] args) {
exam1();
exam2();
Console.ReadKey();
}
public static async Task exam1()  {
await Task.Run(() =>  {
for (int a = 0 ; a < 5 ; a++ )  {
Console.WriteLine(" This will be printed 5 times.");
}
});
}
public static void exam2() {
for (int a = 0 ; a < 2 ; a++ ) {
Console.WriteLine(" This statement will be printed 2 times. ");
}
}
}
Copy after login

Output:

C# await

Code Explanation: For our first example, we have two different methods which are totally independent and we have called these methods from the main method of the program. Starting with the required system files, we have our class program with the main method, the main method within which we call the two methods that are to be declared. Our first method exam1 is initiated, with the await keyword followed by a lambda function and for a statement to print an output statement. Then we have our second method exam2, which has a for a statement, which will print the output statement for a number of times.

Example #3

As expected, the output statements are as mentioned in the code, moving on, we will implement the await keyword with another example where we will have three methods and few print statements.

Code:

using System;
using System.Threading.Tasks;
class Program {
static void Main(string[] args) {
Console.WriteLine("\n");
callMethod();
Console.ReadKey();
}
public static async void callMethod() {
Task<int> task = exam1();
exam2();
int total = await task;
exam3(total);
}
public static async Task<int> exam1() {
int total = 0;
await Task.Run(() =>
{
for (int a = 0 ; a < 10 ; a++ )  {
Console.WriteLine(" This is a simple statement. ");
total += 1;
}
});
return total;
}
public static void exam2()  {
for (int a = 0 ; a < 2 ; a++ )  {
Console.WriteLine(" This is a second output statement.");
}
}
public static void exam3(int total) {
Console.WriteLine(" Total number of statements from first method are   " + total);
}
}
Copy after login

Output:

C# await

Code Explanation: for our third example, we have three different methods and we call these methods from the main method. With our main method, we call the callMethod, which has has a task to call for the exam1 method and so the exam1 is executed. As per the program, here the exam1 method will not be executed first as it has the await keyword, so it will move to the next method which is exam1 and the exam2 method will be executed, which is a statement, printed twice as mentioned in the for a loop. Then our exam1 method will be printed followed by the exam3, the exam3 method is a single statement. Totally, a 13 output statement will be printed. The last statement will be printing the number of times we print the statement from the exam1 method. As explained the total number of 13 print statements, simple execution of await keyword with multiple methods.

Conclusion

To conclude, the await keyword is used to suspend events and bring back control of the program to the caller. This functionality is widely used when we have multiple methods and functions. The await keyword is used to return the control back to the caller and it can only be applied within the async method.

The above is the detailed content of C# await. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template