In This Blog, you are going to learn one of the most important concept of Csharp programming that is Async And Await Keywords and also Synchronous & Asynchronous Programming. These 2 keywords are very precious when using asynchronous operatios.




ASYNC & AWAIT IN C#
- In C# Programming, Async and Await are the two keywords that help us to program asynchronously.
- In C# Programming, An async keyword is a method that performs asynchronous tasks such as fetching data from a database, reading a file, etc, they can be marked as “async”.
- In C# Programming, await keyword making “await” to a statement means suspending the execution of the async method it is residing in until the asynchronous task completes.
- So after suspension, the control goes back to the caller method.
- So once the task completes, the control comes back to the states where await is mentioned and executes the remaining statements in the enclosing method.
Task Class In C#
- In C# Programming, the Task class will always represent a single operation and that operation will be executed asynchronously on a thread pool thread rather than synchronously on the main thread of the application.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncAwaitDemo
{
internal class Program
{
static void Main(string[] args)
{
Task1();
Task2();
Task3();
Task4();
Console.ReadLine();
}
public static async void Task1()
{
await Task.Run(() =>
{
Console.WriteLine("Task 1 Starting..");
Thread.Sleep(4000);
Console.WriteLine("Task 1 Completed..");
});
Console.WriteLine("Hello Adil....");
}
public static async void Task2()
{
await Task.Run(() =>
{
Console.WriteLine("Task 2 Starting..");
Thread.Sleep(2000);
Console.WriteLine("Task 2 Completed..");
});
}
public static async void Task3()
{
await Task.Run(() =>
{
Console.WriteLine("Task 3 Starting..");
Thread.Sleep(5000);
Console.WriteLine("Task 3 Completed..");
});
}
public static async void Task4()
{
await Task.Run(() =>
{
Console.WriteLine("Task 4 Starting..");
Thread.Sleep(1000);
Console.WriteLine("Task 4 Completed..");
});
}
}
}
Download Notes Of This Blog From the link given below
https://www.mediafire.com/file/3w7ltny3p3qb0d6/ASYNC+AWAIT+C#.pptx/file
Download Source Code Of This Blog From the link given below
https://www.mediafire.com/file/acdnvr4cocrc6w6/ASYNC+AWAIT+CSHARP.txt/file
No responses yet