Understanding of Async and Await Keywords in C# and how to achieve asynchronous programming mechanism in C#.
Today we will understand of Async and
Await in C# and also want to clarify some myths about Async.
During the interview most of us faced questions like, what is Async and Await in C#, why we use Async and Await in C#?, how Async and Await are work in C#. Even in real projects lots of developers using this keyword but some of them don’t know all about this keyword. I’m telling some of them may be possible all of them know about these keywords, so please forgive me if you know. In this article, I will give all the answers to the most of questions regarding Async and Await keywords.
[Top 100+ popular C# Interview Questions and Answers.]
Definition of Async and Await
Async and Await are the code markers,
which indicates where the control should resume after task completion.
Async and Await Key points
Async and Await are the code markers, which indicate where the controller should resume after the task completion.
Introduce in .Net Framework 4.5.
We can run all the methods parallels using Async and Await keyword.
In the traditional method, we are using Thread mechanisms to achieve parallel processing but it will block the whole UI until all processes not done. We can resolve this problem using simple Async and Await keyword.
If method one is dependent on method two, in that case, the Await keyword is waiting for the completion of method one after that method two will start.
We can’t use Async without the Await keyword or vise-versa in the code.
When we use Async and Await?
In
real-application when there are long-running tasks like reading or writing
files or any other task, in that case, we use an asynchronous programming
mechanism. We write our code within the Async method and call them into the
main class so it will work parallel with other methods in the main class. We
will look ahead with code examples for the same.
How Async and Await work?
Async will run your long task parallelly
with other tasks. It will divide your code into a chunk of code. Compiler processing
parallels on this chunk of code and produces output. Please keep in mind Async
never creates a new thread for the task, so many developers think that Async
assigns a new thread to the task but that is totally wrong thinking. The Async
task or method runs on the current synchronization context and uses time on the
thread only when the method is active. There is only a working thread is
generated for resume control on to main thread after completion of the task.
Advantage of Async and Await
The main advantage of using Async and Await
is to overcome the problem of GUI blocking. In a traditional threading
mechanism, the main thread is also blocking during the asynchronous process.
Using Async and Await, asynchronous tasks work separately without blocking the
main thread and that is the reason the user can access the GUI.
Examples of C# Async and Await
class Program
{
static void Main(string[] args)
{
Method1();
Console.WriteLine("Main thread.");
Console.ReadLine();
}
public static void Method1()
{
Task.Run(new Action(LongTask));
Console.WriteLine("Long task over.");
}
public static void LongTask()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
}
}
Output:
Here we can clearly see that the controller doesn’t wait for the completion of a long task and move for the next task in the code.
Now suppose some method is dependent on
long-running methods output, then how can we achieve?
Let’s modify the same example with Async and Await keyword, and look at what’s happening.
static void Main(string[] args)
{
Method1();
Console.WriteLine("Main thread.");
Console.ReadLine();
}
public async static void Method1()
{
await Task.Run(new Action(LongTask));
Console.WriteLine("Long task over.");
}
public static void LongTask()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
}
Output:
Here we can clearly see that the compiler is waiting for the completion of a long-running task after that move forward to the next task. During this process, the main thread is a non-blocking state so the user can access the GUI during this asynchronous process. Using the Await keyword we can wait until the result of a long-running task.
Myth of C# Async and Await.
Some of the developers think that
asynchronous process produces new threads, but in actual the asynchronous
process never create or produce new threads please keep in mind. As I have
mentioned above, The Async method runs on the current synchronization context
and uses time on the thread only when the method is active. There is only a
working thread is generated for resume control on to main thread after
completion of the task.
I hope you have liked this article to the understanding of Async and Await in C#.
Please
feel free to comment if you have found anything incorrect, or you want to share
more information about the topic discussed above.
Please
Share this article so it will help to another one because “Helping millions grow better”.
Follow my blog https://mydotnetguru.blogspot.com to read the more interesting technical articles.
Next Recommended Article:
- When to use abstract class and interface in C#?
- Optimized top 2 ways to find 2nd or Nth highest salary in SQL Server.
- Different ways to remove or delete duplicate rows from a SQL Table.
- Top 100+ popular ASP.Net MVC Interview Questions and Answers.
- Top 100+ popular SQL Interview Questions and Answers Blog.
- @Input, @Output decorator and EventEmitter class in Angular.
- Dependency Injection and types of dependency injection.
No comments:
Post a Comment