Synchronous: Suppose I am assigned 4 tasks (the first task has a range of 4 seconds, the second task has a range of 2 seconds, the third task has a range of 5 seconds and the last one has only one second) then in synchronous programming the one I assign first that will be finished first, then the second one will start working. This is how all of them will run one after the other. Since I have assigned four tasks then in synchronous programming my time will take a total of 12 seconds (4s+2s+5s+1s). Here is the code for synchronous in c#,
static void Main(string[] args) {
// Synchronous task
Task1();
Task2();
Task3();
Task4();
Console.ReadLine();
}
public static void Task1() {
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Started task 1...");
Task.Delay(4000).Wait();
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Completed task 1!");
}
public static void Task2() {
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Started task 2...");
Task.Delay(2000).Wait();
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Completed task 2!");
}
public static void Task3() {
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Started task 3...");
Task.Delay(5000).Wait();
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Completed task 3!");
}
public static void Task4() {
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Started task 4...");
Task.Delay(1000).Wait();
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Completed task 4!");
}
Asynchronous: (āĻāĻāĻžāύā§āĻ āĻā§āĻŦāϞ async āĻāĻŦāĻ await āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻšā§āĨ¤ āϝā§āĻāĻžāύ⧠await in asynchronous methods in C# is essential because it allows the method to suspend its execution until the awaited operation completes, without blocking the calling thread. This is crucial for non-blocking, responsive, and efficient code in scenarios where you have long-running operations such as I/O-bound tasks or network requests. āϝāĻžāϰ āĻŦāĻžāĻāϞāĻž āĻāϰāϞ⧠āĻĻāĻžāĻā§āĻžā§, C#-āĻ āĻ ā§āϝāĻžāϏāĻŋāĻā§āĻā§āϰā§āύāĻžāϏ āĻĒāĻĻā§āϧāϤāĻŋāϤ⧠await āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāĻž āĻ āĻĒāϰāĻŋāĻšāĻžāϰā§āϝ āĻāĻžāϰāĻŖ āĻāĻāĻŋ āĻāϞāĻŋāĻ āĻĨā§āϰā§āĻĄ āĻŦā§āϞāĻ āύāĻž āĻāϰ⧠āĻĒā§āϰāϤā§āĻā§āώāĻŋāϤ āĻ āĻĒāĻžāϰā§āĻļāύ āϏāĻŽā§āĻĒā§āϰā§āĻŖ āύāĻž āĻšāĻāϝāĻŧāĻž āĻĒāϰā§āϝāύā§āϤ āĻĒāĻĻā§āϧāϤāĻŋāĻāĻŋāĻā§ āϤāĻžāϰ āĻāĻžāϰā§āϝ āϏāĻŽā§āĻĒāĻžāĻĻāύ āϏā§āĻĨāĻāĻŋāϤ āĻāϰāϤ⧠āĻĻā§āϝāĻŧāĨ¤ āĻāĻāĻŋ āĻāĻŽāύ āĻĒāϰāĻŋāϏā§āĻĨāĻŋāϤāĻŋāϤ⧠āĻ -āĻŦā§āϞāĻāĻŋāĻ, āĻĒā§āϰāϤāĻŋāĻā§āϰāĻŋāϝāĻŧāĻžāĻļā§āϞ āĻāĻŦāĻ āĻĻāĻā§āώ āĻā§āĻĄā§āϰ āĻāύā§āϝ āĻ āϤā§āϝāύā§āϤ āĻā§āϰā§āϤā§āĻŦāĻĒā§āϰā§āĻŖ
āϝā§āĻāĻžāύ⧠āĻāĻĒāύāĻžāϰ āĻĻā§āϰā§āĻ-āĻāϞāĻŽāĻžāύ āĻā§āϰāĻŋāϝāĻŧāĻžāĻāϞāĻžāĻĒ āϝā§āĻŽāύ I/O-āĻŦāĻžāĻāύā§āĻĄ āĻāĻžāϏā§āĻ āĻŦāĻž āύā§āĻāĻāϝāĻŧāĻžāϰā§āĻ āĻ āύā§āϰā§āϧ āϰāϝāĻŧā§āĻā§āĨ¤) On the other hand, in an asynchronous program, if I am assigned 4 tasks (the first task has a range of 4 seconds, the second task has a range of 2 seconds, the third task has a range of 5 seconds and the last one has only one second) then in the asynchronous program all the tasks start at once, then the one with the lowest that oneâs work is completed first, while the others also finish a little. In this way all the tasks are finished by the time it takes 5 seconds at the most. Whereas synchronous takes 12 seconds but asynchronous takes only 5 seconds. Here is the code for asynchronous in c#,
static void Main(string[] args) {
// Asynchronous task
Task5();
Task6();
Task7();
Task8();
Console.ReadLine();
}
public static async void Task5() {
await Task.Run(() => {
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Task 5 starting...");
Thread.Sleep(4000);
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Task 5 completed!");
});
}
public static async void Task6() {
await Task.Run(() => {
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Task 6 starting...");
Thread.Sleep(2000);
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Task 6 completed!");
});
}
public static async void Task7() {
await Task.Run(() => {
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Task 7 starting...");
Thread.Sleep(5000);
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Task 7 completed!");
});
}
public static async void Task8() {
await Task.Run(() => {
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Task 8 starting...");
Thread.Sleep(1000);
Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Task 8 completed!");
});
Console.WriteLine("Check all task exist or not!");
}
For youtube video help click here
For my video click here