Sunday, August 28, 2011

DEV303 Making Asynchronous Programming Easy

Jeremy Boyd
Apologies that this post is fairly nonsensical. I've put my raw NZ TechEd 2011 notes up here for my reference. I'd like to think that I'll refine them over time, but that probably won't be the case.
Synchronous -> Wait for result before returning

Asynchronous ->Return now, but call back with result

Async benefits

 UI responsive: Frees UI thread for interaction
 Server scalability: Thread can be reused for other requests

Connected Systems - Increasing latency due to service calls
Use Async only frameworks -JavaScript and Silverlight

Rise of Async only environments
 Node.js - Promotes a totally async environment
 What is Node.js?

Next version of C# will have features specifically for asynchronous coding.
IAsyncResult Pattern (APM)

.NET 2
Event Based Async Pattern (EAP)
 Method name ends in Async

New Extensions
 Task based Async Pattern (TAP)
 New keywords async and await
 Already available in .NET 4. - Task Parallel Library

Goals:
 Compile time code generation into async patterns
 Supports cancellations
 Easy concurrency management

await keyword
 - used in front of an async call.

async keyword
 - indicates to the compiler that this body of code will run async.

Task - represents the body of work being done.
 Task
 CancellationToken
 taskMethodNameAsync

Compiler is transforming code into the Task Asynchronous Pattern
Task performs initial setup and then installs a callback at await.

.MoveNext()

So its like an async within an async...

Handling Progress

Progress

Handling Cancellation

CancellationTokenSource

Support will go through the framework and to WPF, Silverlight, etc...

Only available in full .NET framework at this time.
 Planned to be supplied in `asynchronous work.

Parallel LINQ (PLINQ)

.AsParallel() extension
Utilizes parallel hardware for LINQ queries
 Partitions and merges data intelligently
 Supports all .NET Standard Query Operators
 Works for any IEnumerable
 
 Built on top of the Task Parallel Library.

.AsOrdered()

Try using .AsParallel() when working with LINQ over in memory objects.

TIP: Async CTP requires a clean install of VS2010 SP1 to install into.