I came up with practical asynchronous programming relatively recently, and, studying the topic in more detail, the question came up:

When creating a task using Task.Factory.StartNew (with the TaskCreationOptions.LongRunning parameter), the thread for the task is NOT selected from the pool.
What thread is selected when creating with TaskCompletionSource - from a pool or not from a pool?


PS I read, they say, these two methods are equivalent ... But I want to clarify a very important detail: which stream is selected in the second method?

    2 answers 2

    No.

    Task is not necessarily "code running in some thread." This is not an abstraction over a method running in an incomprehensible where. This is only a formal promise to ever provide a result. Whether there is one thread working on this result, several, or none at all is an internal detail hidden inside Task 'and inaccessible to the observer.

    Task.Run created via Task.Run or TaskFactory.CreateNew are really a method running in a particular thread. But this is not a common property of all tasks. Task has the right not to run anywhere.

    For example, you can create a TaskCompletionSource , start a timer, and when the timer arrives, end the task. In this case, of course, your task will not run in any stream .


    Additional reading on the topic: There is no flow (Stephen Cleary, translated by Andrey Chasovskikh ).

    • So after all, when we create a Task, this task will be performed in which (in some) or thread from the pool. And what about TaskCompletionSource? Also, but with the "chips" of this class about locks? - khirnick
    • @khirnick: Updated the answer, read. - VladD
    • thanks for the answer. The question is ripe: if we create a TaskCompletionSource and set a “long-lasting” function on SetResult, then in what thread will this SetResult be executed? - khirnick
    • 3
      @khirnick: And what does "setResult put a" long-lasting "function on? What does it mean to put a function? - VladD
    • @khirnick all TaskCompletionSource methods (and their continuations) are executed in the current thread, because it is just a wrapper over the task, which has nothing to do with threads. It is needed, for example, where you need to set the "new" asynchronous API in the presence of the "old" (see answer sp7). - andreycha

    The TaskCompletionSource<T> class is a class that allows you to create a Task task, which you control as a puppet. It can be done at any time successfully completed or an exception is written to it, and thus it is said that it ended with an error. This class can be used, for example, when translating from the IAsyncResult pattern to TAP .

      public static Task<IPHostEntry> GetHostEntryAsync(string hostNameOrAddress) { var tcs = new TaskCompletionSource<IPHostEntry>(); Dns.BeginGetHostEntry(hostNameOrAddress, asyncResult => { try { IPHostEntry result = Dns.EndGetHostEntry(asyncResult); tcs.SetResult(result); } catch(Exception e) { tcs.SetException(e); } }, null); return tcs.Task; }