THE FUTURE OF C# AND VISUAL BASIC C# and VB Evolution C# + VB v.Next C# 4.0 + VB 10.0 C# 3.0 + VB.
Download
Report
Transcript THE FUTURE OF C# AND VISUAL BASIC C# and VB Evolution C# + VB v.Next C# 4.0 + VB 10.0 C# 3.0 + VB.
THE FUTURE OF C# AND
VISUAL BASIC
C# and VB Evolution
C# + VB v.Next
C# 4.0 + VB 10.0
C# 3.0 + VB 9.0
C# 2.0 + VB 8.0
C# 1.0 + VB 7.0
Asynchronous Programming
Dynamic + Language Parity
Language Integrated Query
Generics
Managed Code
Trends
Declarative
Dynamic
Concurrent
Asynchronous Programming
• Increasingly connected applications
– More latency
– More UI responsiveness problems
– More scalability issues
• Asynchronous programming
– Becoming the norm in responsive, scalable apps
– Async-only APIs, e.g. JavaScript and Silverlight
Asynchrony in a Nutshell
• Synchronous Wait for result before returning
– string DownloadString(...);
• Asynchronous Return now, call back with result
– void DownloadStringAsync(..., Action<string> callback);
• Sharing Threads between activities
– UI responsiveness: Free UI thread for user interaction
– Server scalability: Reuse thread for other requests
Synchronous vs. Asynchronous
var data = DownloadData(...);
ProcessData(data);
DownloadDataAsync(... , data => {
ProcessData(data);
});
Synchronous vs. Asynchronous
var data = DownloadData(...);
ProcessData(data);
DownloadDataAsync(... , data => {
ProcessData(data);
});
Asynchronous Responsive UI
DEMO
Visual Studio Async CTP
http://msdn.com/vstudio/async
ANNOUNCING
Asynchronous Methods
• Goal: Just like synchronous programming
– You can have your cake and eat it too!
• Framework: Use Task<T> for all asynchrony
– Add Task<T> support to existing .NET and Silverlight APIs
– Easy for you to do the same
• Languages: Asynchronous Methods
– “async” modifier marks method or lambda as asynchronous
– “await” operator yields control until awaited task completes
Task<T>
• Represents “ongoing operation”
– Could be async I/O, background worker, anything...
– Single object for status, result and exceptions
• Composable callback model
– var task2 = task1.ContinueWith(t => … t.Result …);
– The “await” operator rewrites to continuations
• Combinators
– WhenAll, WhenAny, Delay, etc.
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
Asynchronous Control Flow
async void DoWorkAsync() {
var t1 = ProcessFeedAsync("www.acme.com/rss");
var t2 = ProcessFeedAsync("www.xyznews.com/rss");
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);
}
How Does it Work?
async Task<XElement> GetRssAsync(string url) {
var client = new WebClient();
var task = client.DownloadStringTaskAsync(url);
var text = await task;
var xml = XElement.Parse(text);
return xml;
}
How Does it Work?
Task<XElement> GetRssAsync(string url) {
var $builder = AsyncMethodBuilder<XElement>.Create();
var $state = 0;
TaskAwaiter<string> $a1;
async Task<XElement> GetRssAsync(string
url) {
Action $resume
= delegate {
var client = new WebClient(); try {
var task = client.DownloadStringTaskAsync(url);
if ($state == 1) goto L1;
var text = await task;
var client = new WebClient();
var xml = XElement.Parse(text);
var task = client.DownloadStringTaskAsync(url);
return xml;
$state = 1;
}
$a1 = task.GetAwaiter();
if ($a1.BeginAwait($resume)) return;
L1: var text = $a1.EndAwait();
var xml = XElement.Parse(text);
$builder.SetResult(xml);
}
catch (Exception $ex) { $builder.SetException($ex); }
};
$resume();
return $builder.Task;
}
Asynchronous Web Servers
DEMO
Unifying Asynchrony
• An asynchronous scenario
– Scrape YouTube for video links
– Download two or more videos concurrently
– Create a mashup from downloaded videos
– Save the resulting video
Unifying Asynchrony
try {
string[] videoUrls = await ScrapeYoutubeAsync(url);
Task<Video> t1 = DownloadVideoAsync(videoUrls[0]);
Task<Video> t2 = DownloadVideoAsync(videoUrls[1]);
Video[] vids = await Task.WhenAll(t1, t2);
Video v = await MashupVideosAsync(vids[0], vids[1]);
await v.SaveAsync(textbox.Text);
}
catch (WebException ex) {
ReportError(ex);
}
// Network-bound
// Start two downloads
// Wait for both
// CPU-bound
// IO-bound
Asynchronous Methods
• More responsive UI, more scalable servers
• Unifies computational, network and I/O asynchrony
• No more callbacks!
Update – Compiler as a Service
Class
Language
Object Model
public
Read-Eval-Print
Loop
Foo
Field
DSL Embedding
Metaprogramming
private
Source Code
string
C# Compiler
x
.Net Assemblies
Compiler as a Service
DEMO
http://msdn.com/vstudio/async
http://twitter.com/Pilchie
[email protected]
QUESTIONS?