Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To make things asynchronous you generally compose it together with other things that are asynchronous. If you are only doing synchronous things with your asynchronous methods then it doesn't make any sense to use the async pattern. For example, if all your code does is put work on the thread pool you haven't accomplished anything because you've handed back the thread to the thread pool by moving your code asynchronous but stolen it right back from ASP.NET by launching your work there.</p> <p>If you are making a database call to get the user, and your database supports asynchronous operations, you might build something like this:</p> <pre><code>public IAsyncResult BeginGetUser(AsyncCallback callback, object state) { var taskFunc = Task&lt;DbReader&gt;.Factory.FromAsync(db.BeginGetUser, db.EndGetUser); return taskFunc.ContinueWith(task =&gt; { var reader = task.Result; reader.Read(); return new Data.User { DisplayName = reader["displayName"] as string, UserId = Guid.Parse(reader["userId"] as string), } } ); } public Result&lt;Data.User&gt; EndGetUser(IAsyncResult asyncResult) { return (Task&lt;User&gt;)(asyncResult).Result; } </code></pre> <p>Let me stress something: Async programming is tricky. With Task it gets a little easier, but you still have to wrap your head around continuations. Debugging is a chore and one wrong move and your code just disappears and you don't know why. If you miss an exception it will appear on the finalizer thread and you'll not necessarily know why. Also, in order to do async programming you have to tip-toe into multi-threaded programming which is full of danger.</p> <p><em>Make sure you really understand what is going on before attempting to go Asynchronous.</em> If your code is synchronous by nature then making it asynchronous doesn't buy you very much <em>unless</em> it's a long-running process and you are OK spinning up another thread to handle it. People use asynchronous handlers so that IIS (or whatever underlying web server) can have its thread back to service other requests; as I mentioned before, if you then just go take a thread off the thread pool you are stealing that thread back from IIS and you won't see any gains in scalability. If you just wildly create new threads, however, you open yourself up to denial of service attacks. </p>
    singulars
    1. This table or related slice is empty.
    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. 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