Info
Breakout Session :: 400 (Expert) :: Developer Tools, Languages and Frameworks
“Asynchronous programming is no longer an option; it’s become a must on various platforms, including Silverlight, Windows Phone 7, and various data-centric frameworks. Unfortunately, dealing with asynchrony is way too hard in today’s world of development tools and frameworks. The huge amount of manual and error-prone plumbing leads to incomprehensible and hard to maintain code. As we reach out to services in the cloud, the desire for asynchronous computation is ever increasing, requiring a fresh look on the problems imposed by reactive programming. In this session, we explore various methodologies to address asynchronous programming, and explain how they relate and differ. First, we’ll explore existing patterns and libraries – such as the TPL – to sketch some of the pain-points. Armed with this knowledge, we’ll approach the problem from different angles, including a language-centric view with F#’s asynchronous workflows and the upcoming asynch and await features in C# and Visual Basic. Next, we’ll move beyond sequential composition of asynchronous computations, and introduce the Reactive Extensions (Rx) that enable you to express rich queries – even using LINQ syntax – over asynchronous push-based “reactive” event streams.”
Highlights
The discussion starts by introducing the concepts of Asynchrony (multiple parties evolving in time independently), Concurrency (order in which multiple tasks execute is not determined) and Parralelism (multiple tasks working together), and some wording (“Reactive”, “Callbacks”, “Events”) that typically describe asynchronous situations.
Abstractions in .Net that support asynchrony are: Asynchronous Programming Model (uses callback functions), Event-Based Asynchronous Pattern (uses completion events), and the BackgroundWorker Component.
New tools and abstractions being introduced in upcoming versions of C# and .Net include:
await keyword suspends the code that follows the keyword into a continuation for the asynchronous operation completion. The keyword (and the code following it) can be safely wrapped in standard control statements such as while{}, or try{} catch{} blocks.
Task<T> is a representation of computations (.StartNew()), and hooks (.ContinueWith()) for continuations (which allows for sequencing). Task.Result blocks the calling thread.
BeginVerb-EndVerb patterned functions and VerbAsync functions are complemented with VerbTaskAsync functions that return Task<T>.
Some code examples:
Example 1
1
2
3
| var report = Task.Factory.StartNew(() => GetFileData())
.ContinueWith(x => Analyze(x.Result))
.ContinueWith(y => Summarize(y.Result)); |
Example 2
1
2
3
4
5
6
7
8
9
10
11
12
13
| async void DoWorkAsync() {
var t1 = ProcessFeedAsync("x");
var t2 = ProcessFeedAsync("y");
await Task.WhenAll(t1, t2);
DisplayMessage("Done");
}
async Task ProcessFeedAsync( string url ) {
var text = await DownloadFeedAsync(url);
var doc = ParseFeedIntoDoc(text);
await SaveDocAsync(doc);
ProcessLog.WriteEntry(url);
} |
Reactive Extension (Rx)
Rx is a library for composing asynchronous and event-based programs using observable sequences. Think IObservable<T> and IObserver<T> (subscribe, push) vs. IEnumerable<T> and IEnumerator<T> (pull, blocking).
The concepts introduced by Rx are best studied through additional research into some of the functions it provides:
.Throttle()
.DistinctUntilChanged()
.TakeUntil()
.ObserveOn()
.Subscribe()
The relationship between the various paradigms is best visualized on a graph representing (Axis Y): Single Value vs. Multiple Values over (Axis X): Syncrhonous vs. Asynchronous … the four distinct quadrants of this graph are:
- Single-Value Synchronous:
Func<T>
- Single-Value Asynchronous:
Task<T> (and await)
- Multiple-Value Synchronous:
IEnumerable<T>
- Multiple-Value Asynchronous:
IObservable<T> (and .Subscribe())