Async and Await Query

amanjainits06

Member
Joined
Feb 26, 2022
Messages
5
Programming Experience
3-5
Why the output is like this
123
XYZ
as per my understanding, it should print XYZ first, as I am not writing await in line no. 50


1645898577852.png
 
Also try out the following code which is similar to your post #9.
C#:
using System;
using System.Threading;
using System.Threading.Tasks;

class Program
{
    public static void PrintData()
    {
        Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: PrintData in");
        Test1();
        Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: PrintData out");
    }

    public static async Task Test1()
    {
        Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: Test1 in");
        await Test2();
        Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: Test1 out");
    }

    public static async Task Test2()
    {
        Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: Test2 in");
        // await Task.Delay(1000);
        Thread.Sleep(1000);
        Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: Test2 out");
    }

    public static void Main(string[] parmeetr)
    {
        Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: Main In");
        PrintData();
        Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: Main Out");

        Console.ReadLine();
    }
}

Notice that everything is running on the primary thread 1.

Next uncomment out line 24 and comment out line 25, and try running it again. Notice that some parts run on another thread when the await on line 24 returns.
 
@Skydiver Thanks for your response, can you please help me to understand the meeting of "yields control to its caller" I am not understanding this line, also if I want from method Test2() current running thread to yield control and return a task back up how this can be possible?
Perhaps ypu should dispense with the theoretical examples and tell us what part of async/await you're stuck on understanding, If you get too deep into it to start with, you won't be able to see the wood for the trees/you'll be too close to the detail to appreciate the overall picture.

Console apps are frequently quite bad at teaching async/await because they don't obviously do anything else while they wait. It's easier to explain in a Windows Forms app, where one thread draws the UI and also runs your click handler code. If your click handler code takes too long it jams the UI, because the thread is busy doing your code, not its usual job that it performs outside your code (drawing the UI),

If you have some long running awaitable operation and you await it, then the thread stops doing your code, saves what it was doing, saves the variables etc, and goes off and does the other thing outside your code (drawing the UI, responding to button clicks etc). This part is pivotal to what SkyDiver said about "yield control to the caller" - when C# "stops doing your code" it does so at the point it hits the await and at that point it travels back up the call stack and out the top of your code (or, if you didn't await, then it carries on from the point you should have awaited but didn't). It doesn't jump to the end of the method - it almost behaves like an uncaught exception, stopping doing anything further and just disappearing back up the call stack the way it came in.

When the awaitable operation is done, the thread comes back and restores the saved state, and carries on; that's the difference between this and uncaught expceptions - it has a way to come back and pick up where it left off, with the result of the long running op ready to be used[/icode]
 
Last edited:
Back
Top Bottom