Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I believe the TPL (TaskFactory.Startnew) works similar to ThreadPool.QueueUserWorkItem in that it queues up work on a thread in the thread pool.</p> </blockquote> <p><a href="https://blog.stephencleary.com/2010/08/various-implementations-of-asynchronous.html" rel="noreferrer">Pretty much</a>.</p> <blockquote> <p>From what i've been reading it seems like async/await only "sometimes" creates a new thread. </p> </blockquote> <p>Actually, it never does. If you want multithreading, you have to implement it yourself. There's a new <code>Task.Run</code> method that is just shorthand for <code>Task.Factory.StartNew</code>, and it's probably the most common way of starting a task on the thread pool.</p> <blockquote> <p>If you were dealing with IO completion ports i can see it not having to create a new thread but otherwise i would think it would have to.</p> </blockquote> <p>Bingo. So methods like <code>Stream.ReadAsync</code> will actually create a <code>Task</code> wrapper around an IOCP (if the <code>Stream</code> has an IOCP).</p> <p>You can also create some non-I/O, non-CPU "tasks". A simple example is <code>Task.Delay</code>, which returns a task that completes after some time period.</p> <p>The cool thing about <code>async</code>/<code>await</code> is that you can queue some work to the thread pool (e.g., <code>Task.Run</code>), do some I/O-bound operation (e.g., <code>Stream.ReadAsync</code>), and do some other operation (e.g., <code>Task.Delay</code>)... and they're all tasks! They can be awaited or used in combinations like <code>Task.WhenAll</code>.</p> <p>Any method that returns <code>Task</code> can be <code>await</code>ed - it doesn't have to be an <code>async</code> method. So <code>Task.Delay</code> and I/O-bound operations just use <code>TaskCompletionSource</code> to create and complete a task - the only thing being done on the thread pool is the actual task completion when the event occurs (timeout, I/O completion, etc).</p> <blockquote> <p>I guess my understanding of FromCurrentSynchronizationContext always was a bit fuzzy also. I always throught it was, in essence, the UI thread.</p> </blockquote> <p>I wrote <a href="http://msdn.microsoft.com/en-us/magazine/gg598924.aspx" rel="noreferrer">an article</a> on <code>SynchronizationContext</code>. Most of the time, <code>SynchronizationContext.Current</code>:</p> <ul> <li>is a UI context if the current thread is a UI thread.</li> <li>is an ASP.NET request context if the current thread is servicing an ASP.NET request.</li> <li>is a thread pool context otherwise.</li> </ul> <p>Any thread <em>can</em> set its own <code>SynchronizationContext</code>, so there are exceptions to the rules above.</p> <p>Note that the default <code>Task</code> awaiter will schedule the remainder of the <code>async</code> method on the current <code>SynchronizationContext</code> <em>if it is not null</em>; otherwise it goes on the current <code>TaskScheduler</code>. This isn't so important today, but in the near future it will be an important distinction.</p> <p>I wrote my own <a href="https://blog.stephencleary.com/2012/02/async-and-await.html" rel="noreferrer"><code>async</code>/<code>await</code> intro</a> on my blog, and Stephen Toub recently posted an excellent <a href="http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx" rel="noreferrer"><code>async</code>/<code>await</code> FAQ</a>.</p> <p>Regarding "concurrency" vs "multithreading", see <a href="https://stackoverflow.com/questions/7663101/c-sharp-5-async-await-is-it-concurrent">this related SO question</a>. I would say <code>async</code> enables concurrency, which may or may not be multithreaded. It's easy to use <code>await Task.WhenAll</code> or <code>await Task.WhenAny</code> to do concurrent processing, and unless you explicitly use the thread pool (e.g., <code>Task.Run</code> or <code>ConfigureAwait(false)</code>), then you can have multiple concurrent operations in progress at the same time (e.g., multiple I/O or other types like <code>Delay</code>) - and there is no thread needed for them. I use the term "single-threaded concurrency" for this kind of scenario, though in an ASP.NET host, you can actually end up with "<em>zero</em>-threaded concurrency". Which is pretty sweet.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload