I wrote a simple method to read a file using IAsyncEnumerable<T> and .NET 7.0 (console app):
public static async IAsyncEnumerable<int> ReadIntsAsync(string filePath)
{
    using StreamReader sr = new(filePath);
    while (await sr.ReadLineAsync() is string line)
    {
        if (int.TryParse(line, out int result))
        {
            yield return result;
        }
    }
}
And I'm using it in that way:
static async Task Main(string[] args)
{
    var intList = new List<int>();
    await foreach (var item in ReadIntsAsync("my/file/path/file.txt"))
    {
        intList.Add(item);
    }
    Console.WriteLine(intList.Count);
}
Looking at the Thread windows in the VS I could see the lifecycle of the threads while executing the console app:
- The console app starts with the current thread as the Main thread (for example #1151).
- After starting to iterate over the ReadIntsAsyncthe current thread changed to a new one (for example #87565) and the main thread was changed to "waiting"Waiting on Async Operation, double-click or press enter to view Async Call Stacks.
- The current thread remained #87565 until the program ended.
I expected the current thread to change back to the main thread (#1151) after the iterate over the ReadIntsAsync finished, but it has not happened, why?