11. public async Task<XElement> GetXmlAsync(string url)
{
var client = new HttpClient();
var response = await client.GetAsync(url);
var text = response.Content.ReadAsString();
return XElement.Parse(text);
}
public Task<XElement> GetXmlAsync(string url) {
var tcs = new TaskCompletionSource<XElement>();
var client = new HttpClient();
client.GetAsync(url).ContinueWith(task => {
var response = task.Result;
var text = response.Content.ReadAsString();
tcs.SetResult(XElement.Parse(text));
});
return tcs.Task;
}
21. Class
Meta-programming Read-Eval-Print Loop
public Foo
Language Field
DSL Embedding
Object Model
private X
string
Source .NET
File
Source code Assembly
Source code
Source code Compiler Source code
22. Language Service
Compiler APIs
Compiler Pipeline Metadata
Import
Bart De SmetDeveloper in the SQL division working on asynchronousevent processing systemsHere to talk about future directions in C# and VB
Brief agendaVersions of the languages included in VS 11
Pretty long history with managed languagesIt’s been 11 yearsReleases had a theme to them…Language parity – theme we’re continuingToday: support for the Windows Runtime and asynchronous programmingDrill into it
Parity!Little feature we squeezed in caller info attributesVB 11 more parity with iterators
Asynchronous programming is becoming the normReason it’s important: tablet form factors, touch, responsive UIAsync programming one of the best ways to ensure responsivenessInstead of waiting for return, which could take a long time, spinning toilet bowlsSpecify callbackInterim, app can do other things on the message loopRule of thumb in the WinRT – operation more than 50msLook through the WinRT library code base – how many entry points are async? 10-15% high or not? Tend to be the core operations (the file I/O, the network I/O); rest is setting up properties etc.No matter what you do in your WinRT app, you’ll deal with async
Different ways to represent asynchrony:Task<T> introduced in .NET 4WinRT’sIAsyncOperation<T> interfaceJS promisesObject that represents the operationCan ask for the status, for the result (some cases block)Can also tell the object the callback to continue with when the operation is doneProgress is made through callbacksChallenge – tend to turn code inside out; lambda/function; if you have a loop it gets crazyInsight: possible to automatically transform code in callback styleResearch in CPS programmingEvery programing can be transformed to CTPCommon in certain functional programming languagesWe leverage this insightAsync methods basically use the compiler to transform normal code in callback based state machine
Just looks like regular old method – client, string, parse, return XMLFew things stand out:Async modifier: required to return void or Task<T>Modifier enables to await other asynchronous operationsWhen you hit the await operator; compiler transforms the rest of the method into a callback that it makes the continuation of the GetAsync callThis is in principal what happens (ignoring exception handling etc.):Allocate factory of taskRun up to first awaitMake a lambda out of the rest of the methodAttach it to the async operation you’re awaitingImmediately return a task representing ourselvesWhere the method had a return statement, we stuff the result into the taskCascading effect
Async methods don’t start threads – way of composing asynchrony, not about creating asynchrony as such (orchestrating execution)The asynchrony itself comes from the lowest level function – I/O completion port, etc. yield back to the app, runs the continuation