Solved: Async LINQ – not lazy? Multithreaded?


I have the following code:
It uses System.Linq.Async and I find it difficult to understand. In “classic”/synchronous LINQ, the whole thing would get executed only when I call ToList() or ToArray() on it. In the example above, there is no such call, but the lambdas get executed anyway. How does it work?
The other concern I have is about multi-threading. I heard many times that async != multithreading. Then, how is that possible that the Console.WriteLine(Thread.CurrentThread.ManagedThreadId); prints various IDs? Some of the IDs get printed multiple times, but overall there are about 5 thread IDs in the output. None of my code creates any threads explicitly. It’s all async-await. The StringBuilder does not support multi-threading, and I’d like to understand if the implementation above is valid.
Please ignore the algorithm of my code, it does not really matter, it’s just an example. What matters is the usage of System.Async.Linq.

Best Answer:

ForEachAsync would have a similar effect as ToList/ToArray since it forces evaluation of the entire list.
By default, anything after an await continues on the same execution context, meaning if the code runs on the UI thread, it will continue running on the UI thread. If it runs on a background thread, it will continue to run on a background thread, but not necessarily the same one.
However, none of your code should run in parallel. That does not necessarily mean it is thread safe, there probably need to be some memory barriers to ensure data is flushed correctly, but I would assume these barriers are issued by the framework code itself.

If you have better answer, please add a comment about this, thank you!