Is a new thread created to start // Code after waiting?
May be. Probably no. In the expected implementation of the template for Task , a continuation is performed (a bit after the await expression), using the synchronization context that was "current" when the wait expression was at the beginning. For example, if you are talking about a user interface thread, this means that you will return to the same user interface thread. If you are in a thread stream, you will return to the thread stream thread, but this may be different.
Of course, with your code example, if you are in a user interface thread, your Wait() call will block the user interface thread so that continuation cannot be executed - you need to be careful. (Calling Wait() or Result for tasks that you do not know to complete, and which may require working with the current thread, is a bad idea.)
Note that you can call Task.ConfigureAwait so that you can express your intention not to require a continuation in the same context. This is usually suitable for library methods that do not care about which thread they work in:
await task.ConfigureAwait(false);
(It affects more than a stream - this is the whole context captured or not).
I think it's nice to get to know what is happening under the hood with anticipation. There is a lot of documentation on the Internet, and if you allow me a small plugin, there is also the 3rd edition of C # in depth , and my Tekpub screencast on the topic . Or start with MSDN and get out of there.
source share