C# vNext, VB vNext C#4, VB10 C#3, VB9 C#2, VB8 C#1, VB7 Asynchronous programming Parity LINQ Generics Managed Code.

Download Report

Transcript C# vNext, VB vNext C#4, VB10 C#3, VB9 C#2, VB8 C#1, VB7 Asynchronous programming Parity LINQ Generics Managed Code.

C# vNext, VB vNext
C#4, VB10
C#3, VB9
C#2, VB8
C#1, VB7
Asynchronous programming
Parity
LINQ
Generics
Managed Code
C# vNext, VB vNext
C#4, VB10
C#3, VB9
C#2, VB8
C#1, VB7
Asynchronous programming, ???,
???, ???, ???
Parity, NOPIA, DLR,
covariance, stmt-lambdas
LINQ, lambdas, XML literals, type
inference, extensions
Generics, EnC, My, Disposing, partial,
nullables, XML-comments
Managed Code
http://europe.msteched.com/topic/list/
Iterator Function Mandelbrot(x, y) As IEnumerable(Of PointF)
Dim p As New PointF(0, 0)
While True
Yield p
Dim x2 = p.X * p.X, y2 = p.Y * p.Y
If Math.Sqrt(x2 + y2) >= 2 Then Return
p = New PointF(x2 - y2 + p.X, 2 * p.X * p.Y + p.Y)
End While
End Function
Iterator Function Mandelbrot(x, y) As IEnumerable(Of PointF)
Dim p As New PointF(0, 0)
While True
Yield p
Dim x2 = p.X * p.X, y2 = p.Y * p.Y
If Math.Sqrt(x2 + y2) >= 2 Then Return
p = New PointF(x2 - y2 + p.X, 2 * p.X * p.Y + p.Y)
End While
End Function
Iterator Function Mandelbrot(x, y) As IEnumerable(Of PointF)
Dim p As New PointF(0, 0)
While True
Yield p
Dim x2 = p.X * p.X, y2 = p.Y * p.Y
If Math.Sqrt(x2 + y2) >= 2 Then Return
p = New PointF(x2 - y2 + p.X, 2 * p.X * p.Y + p.Y)
End While
End Function
Guidance: If you want a collection...
' (1) Array literals and Collection Initializers:
' if data is a simple fixed-size array or an initialized List
' (2) Imperative construction of a list:
' UsefulGetFamilyAges()
if it takes detailed
logic to construct
Function
As IEnumerable(Of
Integer)the list
' (3) Query expressions (LINQ):
Return {35, 33, 7, 5, 2}
' Use if
your logic can be expressed
through
query operators
Function
GetFold(IncludeMedia
As Bool) As
IEnumerable(Of
SpecialFolder)
End
Function
' (4)
Iterator
methods:
Dim
folders
As New
List(Of SpecialFolder)
' If you
want deferred evaluation
but
too much for
LINQ
Function
GetFold(useCommon,
useMedia)
As it’s
IEnumerable(Of
String)
folders.Add(SpecialFolder.MyDocuments)
Function
GetFamilyNames()
As
IEnumerable(Of
String)
' Advanced
Dim
paths = features:
From fidd In GetSpecialFolders(useCommon, useMedia)
If
IncludeMedia
Return New List(OfThen
String) From {"Mi", “Di", "Ji", "Bi", "Minnow"}
Iterator
Function
GetDirs(root
As String) As IEnumerable(Of String)
Let
path
= Environment.GetFolderPath(fid)
folders.Add(SpecialFolder.MyPictures)
End Function
' Yield
inside
a Try/Catch
block? – sure! go wild!
Try
Where
IO.Directory.Exists(path)
folders.Add(SpecialFolder.MyMusic)
subdir
In IO.Directory.GetDirectories(root)
Select
folderId.ToString
& "," & path
End If For Each
' Iterator
lambdas?
– need more real-world experience before we
Yield
subdir
Return
paths
Return
folders
can make guidelines...
For Each d In GetDirsRecursive(subdir) : Yield d : Next
Function
EndEnd
Function
Dim str As String
= webClient.DownloadString(uri)
Dim xml As XDocument = XDocument.Parse(str)
The following is wrong. Can you spot the flaw?
“A waiter’s job is to wait on a table until the patrons have finished their meal.
If you want to serve two tables concurrently, you must hire two waiters.”
The following is from the Android developer blog.
Can you spot the flaw?
1. “A good practice in creating responsive
applications is to make sure your main UI thread
does the minimum amount of work.”
2. “Any potentially long task that may hang your
application should be handled in a different
thread.”
3. “Typical examples of such tasks are network
operations, which involve unpredictable delays.”
Dim str As String
= Await webClient.DownloadStringTaskAsync(uri)
Dim xml As XDocument = XDocument.Parse(str)
Dim str As String
= Await webClient.DownloadStringTaskAsync(uri)
Dim xml As XDocument = XDocument.Parse(str)
Asynchrony
is about results that are delayed,
and yielding control while awaiting them
(co-operative multitasking).
Concurrency
is about running or appearing to run two
things at once, through cooperative or
pre-emptive multitasking, or multicore.
Good reasons to use asynchrony:
Good reasons to use concurrency:
• for program control / coordination;
• for UI responsiveness;
• for IO- and network-bound code.
• to kick off indendent IO requests:
“concurrent programs wait faster”;
• to use multicore for CPU-bound things.
Bad reasons to use concurrency:
• “You should stick IO on a background
thread to avoid blocking the UI”
• “Asynchrony is too hard”
Guidance:
' (1) Once it ships, ALL developers should consider async for ALL projects:
' download the CTP now to get a head start!
' (2) RIGHT NOW, adopt the “Task Async Pattern”
Function GetStringAsync(Params, Cancel As CancellationToken) As Task(Of T)
' (3) Use “Function asyncs” for all the async methods you write
Async create
Function
GetStringAsync(...)
As Task(Of T)Programming Model (APM)
' Don’t
anything
new with the Asynchronous
' (4)BeginGetString(Params,
Only use multiple threads
if you
multiple cores
Function
Callback
As need
AsyncCallback,
' i.e.
CPU-bound
tasks
' Use
“Subfor
asyncs”
only
for As
theObject)
very top-level
event handlers
State
As IAsyncResult
Async Sub
Btn1_Click(sender
e As EventArgs)
Handles Btn1.Click
Function
EndGetString(ByVal
ar As
As Object,
IAsyncResult)
As TR
' Don’t create anything new with the Event Asynchronous Pattern (EAP)
Sub GetStringAsync(ByVal Params)
Event GetStringCompleted As GetStringCompletedEventHandler
simpler INotifyPropertyChanged support
compiler as a service...
Dim connectionString = "server=.\sqlexpress;..."
Using con = New SqlConnection(connectionString)
con.Open()
Dim cmd = New SqlCommand("GetSalesFormulas", con)
Using reader = Await cmd.ExecuteReaderAsync()
reader.Read()
Dim formula As String = reader("vb").ToString()
Dim result = Runtime.CompilerServices.Eval(formula)
End Using
End Using
Try
Dim vidUrls As String() = Await ScrapeYoutubeAsync(url)
Dim t1 As Task(Of Video) = DownloadVideoAsync(vidUrls(0))
Dim t2 As Task(Of Video) = DownloadVideoAsync(vidUrls(1))
Dim vids As Video() = Await TaskEx.WhenAll(t1, t2)
Dim v As Video = Await MashupVideosAsync(vids(0), vids(1))
Await v.SaveAsync(textbox1.Text)
Catch ex As WebException
ReportError(ex)
End Try
' Network-bound
' Start 2 dloads
' Wait for both
' CPU-bound
' IO-bound
TASK UNIFIES ALL THESE AREAS; ASYNC TIES THEM TOGETHER
Use Async Functions to compose other tasks, for the “orchestration” of your app
SINGLE-THREADED ASYNCHRONY AND CONCURRENCY
is when we run asynchronous and concurrent tasks with NO additional threads.