In C#,
static async Task<int> Task1()
{
await Task.Delay(1000); // delaying 1 sec
return 10;
}
static async Task<int> Task2()
{
await Task.Delay(2000); // delaying 2 sec
return 10;
}
static async Task<int> Task3()
{
await Task.Delay(3000); // delaying 3 sec
return 10;
}
when performing below operations it takes 6 sec
int a = await Task1();
int b = await Task2();
int c = await Task3();
return a+b+c;
but when performing below operations it takes only 3 sec
var a = Task1();
var b = Task2();
var c = Task3();
return await a + await b + await c;
results are getting as 30 from both the snippet but duration differs...
Can anyone please clarify it
reference - https://dotnetfiddle.net/I31eqK
When you do this:
int a = await Task1();
int b = await Task2();
int c = await Task3();
return a+b+c;
each task is awaited before the next one is started. That is, each task must have returned a result before the next task is awaited. Thus, the total time is the total time for all tasks.
But when you do this:
var a = Task1();
var b = Task2();
var c = Task3();
return await a + await b + await c;
the three tasks are started in parallel, and THEN you await their results. Thus in this case the total time is whatever the longest task takes.
The code below works synchronously. Meaning that you are queuing up the tasks in series (therefore executing them one-by-one) as the functions wait patiently for the ones above them to finish like gentlemen.
Figuratively: "waiting for one to finish their sentence before speaking".
int a = await Task1(); // wait for the above function/Task to complete THEN run this one ("wait for one to finish their sentence THEN talk")
int b = await Task2(); // wait for the above function/Task to complete THEN run this one ("wait for one to finish their sentence THEN talk")
int c = await Task3(); // wait for the above function/Task to complete THEN run this one ("wait for one to finish their sentence THEN talk")
return a+b+c; // return the values received
In the code below, Instead of waiting the function to finish and return one-by-one, you are instead ("asynchronously") waiting for all of the included variables to have a value (as you are not waiting for one function to finish before running the next one, but rather waiting for every necessary value to exist).
Figuratively: "Talking over each other, therefore ending the conversation earlier than waiting for one to finish their sentence".
var a = Task1(); // Run this function while other functions are running ("Talk while other people are talking")
var b = Task2(); // Run this function while other functions are running ("Talk while other people are talking")
var c = Task3(); // Run this function while other functions are running ("Talk while other people are talking")
return await a + await b + await c; // now wait for every variable to have their own value and THEN return
Examples
Here's an example to make this easy to understand:
int a = Task1(); // Asynchronous call ("Dont wait for someone to finish speaking")
int b = Task2(); // Asynchronous call ("Dont wait for someone to finish speaking")
int c = await Task3(); // Synchronous call ("wait for someone/people to finish talking)
The code above should take 5 seconds because:
Task1() pauses for 1000 milliseconds and runs at the same time as Task2() with its 2000 milliseconds (as Task1 and Task2 are not waiting for each other to finish), As Task1 Finishes in 1 second where as Task2 will finish in 2 Seconds, therefore taking it 2 seconds before Task3 is called
Task3(), with its 3000 millisecond pause, will wait patiently until all functions before it are done with their own thing. Because Task2 is finished, Task3 is then called, adding an additional 3 seconds to the 2 seconds Total.
Extra Note: Please run this script (not on dotnetfiddle.com as it doesn't show the live output but rather on your local machine): https://www.toptal.com/developers/hastebin/igapunubis.csharp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With