© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel                             ...
Parallel programming  (API, Performance and Debugging)using VS 2012, C# 5        and .NET 4.5
Task and Continuation
• Task represents a metadata of work unit   • Task.Run(…)   • Task.Factory.StartNew(…)• Task<T> represents a metadata of w...
• A task encapsulates the work item data   –   Execution status                                        Work Unit Context  ...
• Continuations chain tasks one after anothervar tsk = Task.Factory.StartNew(s => …);Task taskAlways = tsk.ContinueWith(so...
Debug and Profiler
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   10
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   11
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   12
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   13
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   14
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   15
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   16
API Improvements
Retargeting to .NET 4.5 will improve performance dramatically.  For example, the code below will run 3-4 times faster.priv...
• Task.Run                                             • Task.Delay   – Quickly schedule new task                         ...
• Less operations fall back to running sequentially.intArray.AsParallel()  .Select(x => Foo(x))  .TakeWhile(x => Filter(x)...
Operators that may cause sequential fallback in both .NET 4 and.NET 4.5 are marked in blue, and operators that may cause f...
• Improve concurrent collections and synchronization.• ConcurrentDictionary is using finer grain synchronization.   • Afte...
• ThreadLocal<T> reduce Values.var resources = new ThreadLocal<int>(trackAllValues: true);Parallel.For (0, 100000, i =>   ...
• Define cancellation with timeout.var ct = new CancellationTokenSource(TimeSpan.FromSeconds(30));// orvar ct = new Cancel...
What is the output of the following Code?var cts = new CancellationTokenSource (TimeSpan.FromSeconds(1));Task a = Task.Run...
• Lazy cancellation will Ignore the cancellation until the  completion of the previous task.Task a = Task.Run(…);Task b = ...
• New overload that allow to pass object state into continuationsvar t = Task<string>.Run(() => "123");t.ContinueWith((tsk...
Scheduler
• TaskCreationOptions and TaskContinuationOptions:   • DenyChildAttach   • HideScheduler.• 3rd Party interaction.• Task.Ru...
Will the UI responsiveness be affected by this code?var scd = TaskScheduler.FromCurrentSynchronizationContext();var tsk = ...
• TaskScheduler provides:   • ConcurrentScheduler   • ExclusiveScheduler• asynchronous equivalent of a reader/writer lock•...
WCF
WCF template support TAP     The proxy default does generate TAP methods for each operation.     At the service side WCF k...
WCF template support TAP     The proxy default does generate TAP methods for each operation.     At the service side WCF k...
C# 5: async / await
async / await                                           .NET 4.5                                                          ...
async / await                                      .NET 4.5                                                       TPL Data...
• async – mark the method as a-sync• await – delegate the execution to a call back thread      markerstatic async Task Exe...
• async – mark the method as a-sync• await – delegate the execution to a call back threadstatic async Task Execute(){     ...
static async Task Execute(){           Console.WriteLine(“run on calling thread”);           await Task.Factory.StartNew((...
static async Task Execute()                                                       Thread Id = 1{           Console.WriteLi...
static async Task Execute(){           Console.WriteLine(“run on calling thread”);           DateTime result = await Task....
• How do we handle Exception in Tpl?static void Execute(){    Console.WriteLine("run on calling thread");    …    var tsk ...
• How do we handle Exception in Tpl?static void Execute(){  try {     Console.WriteLine("run on calling thread");     …   ...
• How do we handle Exception in Tpl?static void Execute(){  try {     Console.WriteLine("run on calling thread");     …   ...
• How do we handle Exception in Tpl?static void Execute(){  try {     Console.WriteLine("run on calling thread");     …   ...
• How do we handle Exception using Async?static async Task Execute(){  try  {          Console.WriteLine(“run on calling t...
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   49
how long does it take to execute the method?private static async Task Execute(){           var sw = Stopwatch.StartNew(); ...
await Task.Delay(1000);         await Task.Delay(1000);Task t = Task.Delay(1000);t.ContinueWith (t1 =>{           Task t2 ...
how long does it take to execute the method?static async Task Execute(){           var sw = Stopwatch.StartNew();         ...
http://blogs.msdn.com/b/pfxteam/archive/2011/09/28/10217876.aspxstatic async Task Execute(){           var sw = Stopwatch....
Task t1 = Task.Delay(1000);Task t2 = Task.Delay(1000);await Task.WhenAll (t1, t2);Task t1 = Task.Delay(1000);Task t2 = Tas...
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   55
private static async Task Execute(){           var sw = Stopwatch.StartNew();         for (int i = 0; i < 10; i++)        ...
private static async Task Execute( int n ){           var sw = Stopwatch.StartNew();          for (int i = 0; i < n; i++) ...
• Using a-sync syntax from UI thread will execute the operation on  other thread and sync back to the UI thread after the ...
Using a-sync syntax from UI thread will execute the operation on    other thread and sync back to the UI thread after the ...
Using a-sync syntax from UI thread will execute the operation on    other thread and sync back to the UI thread after the ...
• Using a-sync syntax from UI thread will execute the operation on  other thread and sync back to the UI thread after the ...
• Code reviewusing (var scope = new MyDisposable()){   …   Task.Run(() => scope.Execute());   …}              © Copyright ...
• the Dispose will be call on the completion thread.using (var scope = new MyDisposable()){   …   await Task. Run(() => sc...
• the Dispose will be call on the completion thread.using (var scope = new MyDisposable()){   …   await Task. Run(() => sc...
• Await can apply Lambda expressionFunc<Task> f = async () =>    {        Console.WriteLine(“Synchronously");        await...
• await cannot be declare in:   –   constructor   –   lock   –   catch or finally   –   unsafe block                © Copy...
Reactive Extension (Rx)
async / await                                      .NET 4.5                                                           TPL ...
Rx (Reactive Extension) is a library for composing        asynchronous and event-based programs        using observable.Rx...
Rx (Reactive Extension) is a library for composing             asynchronous and event-based programs             using obs...
Does it sound familiar?    event EventHandler Click;                           Click += (s,e) => {…};Rx = Push standard ov...
Rx = Linq to Events stream.    - Event cant be composed into LINQ query.    - Event cant be stored in variables and data s...
• Concept• Where to use• How to use                 90
GPS               Accelerometer                                                            Stock TickersSocial Media      ...
NextThread 1                   Thread 2                        ValueNext                                                  ...
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   94
How can we reduce the Azure request costfor Search (send request only if the userinput is idle for 0.5 seconds)           ...
• Google “Rx msdn”       http://msdn.microsoft.com/en-us/data/gg577609       https://rx.codeplex.com• Platform:   –   NET ...
• Google “Rx msdn”      http://msdn.microsoft.com/en-us/data/gg577609     https://rx.codeplex.com• NuGet   –   Rx-Main   –...
Producerpublic interface IObservable <T>{            IDisposable Subscribe (IObserver<T> observer);}                      ...
Producerpublic interface IObservable <T>{            IDisposable Subscribe (IObserver<T> observer);}                      ...
Producerpublic interface IObservable <T>{            IDisposable Subscribe (IObserver<T> observer);}                      ...
Producerpublic interface IObservable <T>{            IDisposable Subscribe (IObserver<T> observer);}                      ...
Producerpublic interface IObservable <T>{            IDisposable Subscribe (IObserver<T> observer);}                      ...
How do you unsubscribe from:            Producer                                          this.Click += (s,e) => {…};publi...
static void Main(string[] args){    IObserver<int> c1 = new Consumer(0);    IObserver<int> c2 = new Consumer(2);    IObser...
IDisposable unsubscribe = foo.Subscribe (value =>    Console.WriteLine(value));The following extension method is available...
static void Main(string[] args){    IObserver<int> c2 = new Consumer(2);    IObservable<int> p = new Producer();    IDispo...
• The marble diagram is the formal way of presenting the  observable stream   on next     on next                         ...
• The marble diagram is the formal way of presenting the  observable stream              © Copyright SELA software & Educa...
var timeStream = Observable.Interval (TimeSpan.FromSeconds(1));var negativeTimeStream =       from value in timeStream sel...
var timeStream = Observable.Interval (TimeSpan.FromSeconds(1));var negativeTimeStream =       from value in timeStream sel...
var evenTimeStream = from value in timeStream           where value % 2 == 0           select value;   1      2           ...
1 Sec 1 Sec 1 Sec 1 Sec• Create time base stream   – Observable.Interval(…);   – Observable.Timer(…);                     ...
• Visual Rx is a free open source tool, which enable  monitoring a real-time Rx datum stream.• http://visualrx.codeplex.co...
• Retry: re-subscribe on error• Catch: subscribe fallback stream on error• Finally: finalization action              © Cop...
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   122
If a tree falls in a forest and no one is around to hear it,does it make a sound?if it did make a sound when nobody observ...
Hot observable streams are streams that are active regardless ofwhether or notthey are being observed.such stream are the ...
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   125
Subject is great for propagation strategy  Can merge multiple subscribers into single stream  Very useful for custom chann...
class Program {                                class Program {    event Action<int> E;                           ISubject<...
class Program {                                class Program {    event Action<int> E;                           ISubject<...
class Program {                                class Program {    event Action<int> E;                           ISubject<...
class Program {                                class Program {    event Action<int> E;                           ISubject<...
class Program {                                class Program {    event Action<int> E;                           ISubject<...
class Program {                                class Program {    event Action<int> E;                           ISubject<...
class Program {                                class Program {    event Action<int> E;                           ISubject<...
class Program {                                class Program {    event Action<int> E;                           ISubject<...
class Program {                                class Program {    event Action<int> E;                           ISubject<...
Can’t pass as a parameter                                           Separation of Concerns:                  or store in a...
• Different strategies for combining the result of multiple streams  together              © Copyright SELA software & Edu...
the merge combinator is used to merge multiple IObservable streams into singleIObservable streams           T1            ...
the merge combinator is used to merge multiple IObservable streams into singleIObservable streams                 var xs =...
the zip combinator is used to synchronize 2 IObservable streams into a singleIObservable stream            S1             ...
the zip combinator is used to synchronize 2 IObservable streams into a singleIObservable stream                    var xs ...
the zip combinator may not be the right choice for non-synchronized stream.            S1      S2         S3          S4  ...
the buffer with time operator buffers values that occur within specifictime windows, and then publishs the buffered values...
the buffer with time operator buffers values that occur within specifictime windows, and then publishs the buffered values...
Day 1                              Day 2Credit: http://enumeratethis.com/2011/07/26/financial-charts-reactive-extensions/ ...
Day 1                                  Day 2                   High                      H                                ...
the Window operator buffers values that occur within specifictime windows, and then publishes the buffered values whenever...
the Window operator buffers values that occur within specifictime windows, and then publishes the buffered values whenever...
Day 1                             Day 2                H                                             H                    ...
• Drag & drop           © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel   156
Data (What)•    Scheduler      – Data      – Threads                                                                      ...
• NewThreadScheduler: new thread execution.• ThreadPoolScheduler: thread pool execution.• TaskPoolScheduler: TPL execution...
Forum activity, similar / overlapping Microsoft technologiesRX                                  PLINQ / Parallel Extension...
Rx is a powerful Linq-able dataflow extensionRx applies to the producer / consumer (pub / sub)patternRx has lots of extens...
17© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel                           ...
http://blogs.microsoft.co.il/blogs/bnaya/Rx at DelLab: http://msdn.microsoft.com/en-us/devlabs/ee794896101 Rx Samples: htt...
TPL Dataflow
async / await                                      .NET 4.5                                                       TPL Data...
• TPL Dataflow (TDF) is a higher-level abstraction over the TPL• It provides common solutions for common parallel patterns...
• TPL Dataflow (TDF) evolved from:   – AAL: Asynchronous Agents Library (native)   – Axum: agent-based language that build...
• High-throughput / low-latency.• Immense CPU and I/O traffic• TDF’s Building blocks can be used:   – Standalone   – In co...
• Download .NET 4.5• Reference: System.Threading.Tasks.Dataflow.dll   – NuGet: Tpl Dataflow or TDF• Main namespaces:   – S...
• DTF is based on 2 interfaces:   – ISourceBlock<T> // producer   – ITargetBlock<T> // consumer                           ...
• OfferMessage:   • Accept or Deny the message   • Calls back to methods on the source block   • consumeToAccept – communi...
OfferMessage communicates back to caller either by using thesource parameter or by the DataflowMessageStatus return value....
• Data-pulling APIs. interface IReceivableSourceBlock<TOutput>    : ISourceBlock<TOutput>{    bool TryReceive(out TOutput ...
LinkTo -> OfferMessage                         Push: LinkToISourceBlock<T>                                                ...
• TPL Dataflow provides built-in blocks for most common and  needed scenarios• The built-in blocks fall into three categor...
• A propagator which provides a buffer for storing instances of T• Non broadcasting nature:   – Push via LinkTo will offer...
private BufferBlock<int> _buffer = new BufferBlock<int>();private void Producer(){         for (int i = 0; i < 10; i++) { ...
private BufferBlock<int> _buffer = new BufferBlock<int>();private void Producer(){                                        ...
private BufferBlock<int> _buffer = new = new ConcurrentQueue<int>();        ConcurrentQueue<int> _buffer BufferBlock<int>(...
private BlockingCollection<int>_buffer = new ConcurrentQueue<int>();        ConcurrentQueue<int> _buffer = new BlockingCol...
private BlockingCollection<int> _buffer = new BlockingCollection<int>();private void Producer(){         for (int i = 0; i...
var abConsumer1 = new ActionBlock<int>(i => …));var abConsumer2 = new ActionBlock<int>(i => …));var bufferBlock = new Buff...
How does it different from Rx?Rx = direct push (functional programming).TDF = push via buffering (agent base).In general i...
• ISourceBlock<T>.AsObservable()• ITargetBlock<T>.AsObserver()var bufferBlock = new BufferBlock<int>();var xs = from item ...
var options = new ExecutionDataflowBlockOptions {BoundedCapacity = 1};var abConsumer1 = new ActionBlock<int>(i => …, optio...
• A propagator which provides a buffer for storing instances of T• Broadcasting nature:   – Always keep the last item   – ...
var abConsumer1 = new ActionBlock<int>(i => …));var abConsumer2 = new ActionBlock<int>(i => …));var broadcastBlock = new B...
• How to avoid over subscription?• How to tune the blocks execution?• Why does the BroadcastBlock is having an input buffe...
• Blocks signaturevar downloader = new ActionBlock<string>(async url =>{    byte [] imageData = await DownloadAsync(url); ...
Action    Broadcast4   3   2   1                                                                    Action                ...
Action    Broadcast4   3   2   1                     1                                                                    ...
Action                                                                           1Broadcast4   3   2                     2...
Action                                                                         2         1Broadcast  4   3                ...
Action                                                                  3     2         1Broadcast     4                  ...
Action                                                               4     3         2Broadcast                        4  ...
Action                                                                     4         3Broadcast                        4  ...
• Can we avoid Task starvation?       MaxMessagesPerTask            © Copyright SELA software & Education Labs Ltd. 14-18 ...
Action    Broadcast4   3   2   1                     1                                                                    ...
Action                                                                           1Broadcast4   3   2                     2...
Action                                                                         2         1Broadcast  4   3                ...
Google: Bnaya Export Web Crawlerhttp://blogs.microsoft.co.il/blogs/bnaya/archive/2012/01/28/tpl-dataflow-walkthrough-part-...
22© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel                           ...
High level abstraction. Producer / Consumer = ISourceBlock / ITargetBlock Greedy vs. Non-Greedy Makes no built-in guarante...
What's new in Visual Studio 2012 General
What's new in Visual Studio 2012 General
Upcoming SlideShare
Loading in...5
×

What's new in Visual Studio 2012 General

610

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
610
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • The Task does not manage the actual scheduling (execution),The execution is actually managed by the scheduler which will discuss on later module-------------------------------------------Instructor note: do explain the Task properties in high level, it will be discuss later in the module.
  • http://blogs.msdn.com/b/pfxteam/archive/2011/11/11/10235999.aspx
  • http://blogs.msdn.com/b/pfxteam/archive/2011/11/11/10235999.aspx
  • using System;using System.Collections.Concurrent;using System.Diagnostics; class Program{    static void Main(string[] args)    {        while (true)        {            var cd = new ConcurrentDictionary&lt;int, int&gt;();            varsw = Stopwatch.StartNew();            cd.TryAdd(42, 0);            for (int i = 1; i &lt; 10000000; i++)            {                cd.TryUpdate(42, i, i – 1);            }            Console.WriteLine(sw.Elapsed);        }    }}
  • http://blogs.msdn.com/b/pfxteam/archive/2012/09/22/new-taskcreationoptions-and-taskcontinuationoptions-in-net-4-5.aspx
  • http://blogs.msdn.com/b/pfxteam/archive/2012/09/22/new-taskcreationoptions-and-taskcontinuationoptions-in-net-4-5.aspxConsole.WriteLine(&quot;Start&quot;);var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));Task a = Task.Run(() =&gt; {Thread.Sleep(3000);Console.WriteLine(&quot;A&quot;); }); Task b = a.ContinueWith( t =&gt; Console.WriteLine(&quot;B&quot;), cts.Token, TaskContinuationOptions.LazyCancellation, TaskScheduler.Default); Task c = b.ContinueWith( t =&gt; Console.WriteLine(&quot;C&quot;));Console.ReadKey();
  • http://blogs.msdn.com/b/pfxteam/archive/2012/09/22/new-taskcreationoptions-and-taskcontinuationoptions-in-net-4-5.aspxContinueWith has a new overloads also allow you to pass object state into continuations, which is important for languages that don’t support closures,useful as a performance enhancement to avoid closures, and quite helpful as a way to associate additional state with a Task (the state is available from the task’s AsyncState property, and is thusly visible in the Parallel Tasks window’s Async State).
  • http://blogs.msdn.com/b/pfxteam/archive/2012/09/22/new-taskcreationoptions-and-taskcontinuationoptions-in-net-4-5.aspxWe’ve also provided more control over task creation. Two new options exist on TaskCreationOptions and TaskContinuationOptions: DenyChildAttach and HideScheduler. These allow you to better author code that interacts with 3rd-party libraries
  • // HideSchedulervar option = TaskContinuationOptions.None;//var option = TaskContinuationOptions.HideScheduler;var scd = TaskScheduler.FromCurrentSynchronizationContext();Write();var a =Task.Factory.StartNew(Write);a.ContinueWith(t =&gt; { Write();Task.Factory.StartNew(Write);Task.Factory.StartNew(() =&gt; Thread.Sleep(10000)); }, CancellationToken.None, TaskContinuationOptions.HideScheduler, scd);
  • concurrent scheduler are able to run as long as there no executing exclusive tasks. As soon as an exclusive task gets scheduled, no more concurrent tasks will be allowed to run, and once all currently running concurrent tasks have completed, the exclusive tasks will be allowed to run one at a time. The ConcurrentExclusiveSchedulerPair also supports restricting the number of tasks that may run concurrently. With that, you can use just the ConcurrentScheduler to schedule tasks such that only a maximum number will run at any given time. Or you could use just the ExclusiveScheduler to run queued tasks sequentially.(31:00)
  • http://msdn.microsoft.com/en-us/library/bb822049.aspxAPM – A-sync Programing ModelEAP – Event A-sync PatternTAP – Task A-sync Pattern
  • http://msdn.microsoft.com/en-us/library/bb822049.aspx
  • ConfigureAwait may prevent dead-lock (wait to completion on UI thread) and boost performance
  • ConfigureAwait may prevent dead-lock (wait to completion on UI thread) and boost performance
  • http://msdn.microsoft.com/en-us/library/bb822049.aspx
  • The interaction dialog between observable and observer id subscription for push data
  • The interaction dialog between observable and observer id subscription for push data
  • The interaction dialog between observable and observer id subscription for push data
  • http://en.wikipedia.org/wiki/First-class_citizenAn object is first-class when it: - can be stored in variables and data structures - can be passed as a parameter to a subroutine - can be returned as the result of a subroutine - can be constructed at run-time / new event MouseMove() - has intrinsic identity (independent of any given name) / GetHashCode()
  • http://en.wikipedia.org/wiki/First-class_citizenAn object is first-class when it: - can be stored in variables and data structures - can be passed as a parameter to a subroutine - can be returned as the result of a subroutine - can be constructed at run-time / new event MouseMove() - has intrinsic identity (independent of any given name) / GetHashCode()
  • can be constructed at run-time / new event MouseMove()has intrinsic identity (independent of any given name) / GetHashCode()
  • Late binding factory with enable side effect (can initialize some other built-in factories like interval with a relevant initialization)Demo: Demos\\Factories\\DeferVsDeferAsyncTest
  •             varxs = Observable.Interval(TimeSpan.FromSeconds(0.3)).Select (_ =&gt; Environment.TickCount);             varys = Observable.Zip(xs, xs, (l, r) =&gt; l == r);             //varxs_ = xs.Publish();            //varys = Observable.Zip(xs_, xs_, (l, r) =&gt; l == r);            //xs_.Connect();                        //varxs_ = xs.Publish().RefCount();            //varys = Observable.Zip(xs_, xs_, (l, r) =&gt; l == r);                        //varys = xs.Publish(xs_ =&gt;            //            Observable.Zip(xs_, xs_, (l, r) =&gt; l == r));             ys.Subscribe(Console.WriteLine);
  • Combine heart –bit with film frame
  • Combine heart –bit with film frame
  • http://msdn.microsoft.com/en-us/library/bb822049.aspx
  • From Introduction to TPL Dataflow by Stephen Toub, Microsoft“Visual Studio 2010 targeted this problem space for native developers with the addition to the C Run-time (CRT) of the Asynchronous Agents Library (AAL). This model has proven to be very attractive and useful, and also shows up in the Concurrency &amp; Coordination Runtime (CCR) available separately as a library contained in the Microsoft Robotics Studio. TDF can be thought of as a logical evolution of the CCR for non-robotics scenarios.”
  • From Introduction to TPL Dataflow by Stephen Toub, Microsoft“Visual Studio 2010 targeted this problem space for native developers with the addition to the C Run-time (CRT) of the Asynchronous Agents Library (AAL). This model has proven to be very attractive and useful, and also shows up in the Concurrency &amp; Coordination Runtime (CCR) available separately as a library contained in the Microsoft Robotics Studio. TDF can be thought of as a logical evolution of the CCR for non-robotics scenarios.”
  • From Introduction to TPL Dataflow by Stephen Toub, Microsoft“Visual Studio 2010 targeted this problem space for native developers with the addition to the C Run-time (CRT) of the Asynchronous Agents Library (AAL). This model has proven to be very attractive and useful, and also shows up in the Concurrency &amp; Coordination Runtime (CCR) available separately as a library contained in the Microsoft Robotics Studio. TDF can be thought of as a logical evolution of the CCR for non-robotics scenarios.”
  • Out of the box TDF provides a handful of ISourceBlock&lt;T&gt; and ITargetBlock&lt;T&gt; implementations for different scenarios.Developers may build their own custom implementations for addressing advanced scenarios.
  • When sources push data to targets, the protocol employed may allow the target to simply accept and keep the offered data, or it may require the target to communicate back to the source. In doing so, sources and targets may participate in a two-phase commit protocol whereby a target can reserve data from a source prior to consuming it. This allows a target to atomically consume data from multiple sources, and is enabled by the ReserveMessage, ConsumeMessage, and ReleaseReservation APIs (if you’re familiar with transactions, you can logically think of these as being related to “prepare,” “commit,” and “rollback,” respectively).consumeToAccept - if true the target block must use ConsumeMessage in order to consume the message value. Most of the built-in building blocks normally passing false.h
  • When sources push data to targets, the protocol employed may allow the target to simply accept and keep the offered data, or it may require the target to communicate back to the source. In doing so, sources and targets may participate in a two-phase commit protocol whereby a target can reserve data from a source prior to consuming it. This allows a target to atomically consume data from multiple sources, and is enabled by the ReserveMessage, ConsumeMessage, and ReleaseReservation APIs (if you’re familiar with transactions, you can logically think of these as being related to “prepare,” “commit,” and “rollback,” respectively).
  • “The built-in blocks fall into three categories: pure buffering blocks, whose primary purposes in life are to buffer data in various ways;execution blocks, which ; and grouping blocks, which group data across one or more sources and under various constraints.”From:Introduction to TPL Dataflow by Stephen Toub, Microsoft.
  • From Introduction to TPL Dataflow by Stephen Toub, Microsoft“Visual Studio 2010 targeted this problem space for native developers with the addition to the C Run-time (CRT) of the Asynchronous Agents Library (AAL). This model has proven to be very attractive and useful, and also shows up in the Concurrency &amp; Coordination Runtime (CCR) available separately as a library contained in the Microsoft Robotics Studio. TDF can be thought of as a logical evolution of the CCR for non-robotics scenarios.”
  • Transform / Transform many blocksRecursive get url -&gt; download -&gt; find links -&gt; outputFilter the link if web page,Like to image download block if image
  • What's new in Visual Studio 2012 General

    1. 1. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel www.sela.co.il
    2. 2. Parallel programming (API, Performance and Debugging)using VS 2012, C# 5 and .NET 4.5
    3. 3. Task and Continuation
    4. 4. • Task represents a metadata of work unit • Task.Run(…) • Task.Factory.StartNew(…)• Task<T> represents a metadata of work unit + return value.Action a = () => Console.WriteLine("Hello World");Task t1 = Task.Run(a);Func<DateTime> f = () => DateTime.Now;Task<DateTime> t2 = Task.Run(f);…DateTime x = t2.Result; 6
    5. 5. • A task encapsulates the work item data – Execution status Work Unit Context – State Delegate – Result State = 123 – Exception Status= Created – Wait Status= WaitingToRun – Cancellation (covered later) Status= Running Status= RanToCompletion Scheduling – Continuation (covered later) Result Status= Faulted Exception Task t = new Task ((state) => Console.WriteLine(“”), 123); t.Start(); 7 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
    6. 6. • Continuations chain tasks one after anothervar tsk = Task.Factory.StartNew(s => …);Task taskAlways = tsk.ContinueWith(sourceTask =>);Task taskError = tsk.ContinueWith(sourceTask => …, TaskContinuationOptions.OnlyOnFaulted); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 8
    7. 7. Debug and Profiler
    8. 8. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 10
    9. 9. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 11
    10. 10. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 12
    11. 11. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 13
    12. 12. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 14
    13. 13. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 15
    14. 14. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 16
    15. 15. API Improvements
    16. 16. Retargeting to .NET 4.5 will improve performance dramatically. For example, the code below will run 3-4 times faster.private static void Test(Task t){ for (int i = 0; i < 1000000; i++) { t = t.ContinueWith(tmp => 1); }} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 18
    17. 17. • Task.Run • Task.Delay – Quickly schedule new task – Schedule task with a delay• Task.FromResult • Task.ContinueWith – Create task from value – More overloads• Task.Yield • Task.WhenAll / WhenAny – Like DoEvents but better – Continueation of WaitAll / WaitAny © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 19
    18. 18. • Less operations fall back to running sequentially.intArray.AsParallel() .Select(x => Foo(x)) .TakeWhile(x => Filter(x)) .ToArray(); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 20
    19. 19. Operators that may cause sequential fallback in both .NET 4 and.NET 4.5 are marked in blue, and operators that may cause fallbackin .NET 4 but no longer in .NET 4.5 are marked in orange. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 21
    20. 20. • Improve concurrent collections and synchronization.• ConcurrentDictionary is using finer grain synchronization. • After upgrading to .NET 4.5, it can run 15% faster.var cd = new ConcurrentDictionary<int, int>();cd.TryAdd(42, 0);for (int i = 1; i < 10000000; i++) cd.TryUpdate(42, i, i – 1); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 22
    21. 21. • ThreadLocal<T> reduce Values.var resources = new ThreadLocal<int>(trackAllValues: true);Parallel.For (0, 100000, i => resources.Value += i);Int total = resources.Values.Sum();resource.Dispose(); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 24
    22. 22. • Define cancellation with timeout.var ct = new CancellationTokenSource(TimeSpan.FromSeconds(30));// orvar ct = new CancellationTokenSource();ct.CancelAfter(TimeSpan.FromSeconds(30)); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 25
    23. 23. What is the output of the following Code?var cts = new CancellationTokenSource (TimeSpan.FromSeconds(1));Task a = Task.Run(() =>{ Thread.Sleep(3000); Console.WriteLine("A");});Task b = a.ContinueWith( t => Console.WriteLine("B"), cts.Token);Task c = b.ContinueWith( t => Console.WriteLine("C")); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 26
    24. 24. • Lazy cancellation will Ignore the cancellation until the completion of the previous task.Task a = Task.Run(…);Task b = a.ContinueWith(…, cancellationToken, TaskContinuationOptions.LazyCancellation, TaskScheduler.Default);Task c = b.ContinueWith(…); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 27
    25. 25. • New overload that allow to pass object state into continuationsvar t = Task<string>.Run(() => "123");t.ContinueWith((tsk, state) => state + tsk.Result, "Some state"); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 28
    26. 26. Scheduler
    27. 27. • TaskCreationOptions and TaskContinuationOptions: • DenyChildAttach • HideScheduler.• 3rd Party interaction.• Task.Run specifies DenyChildAttach and TaskScheduler.Default (ignore current) © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 30
    28. 28. Will the UI responsiveness be affected by this code?var scd = TaskScheduler.FromCurrentSynchronizationContext();var tsk = Task.Factory.StartNew (() => ...);tsk.ContinueWith (t => { // TODO: modify UI component Task.Factory.StartNew ( () => Thread.Sleep(10000) ); }, scd); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 31
    29. 29. • TaskScheduler provides: • ConcurrentScheduler • ExclusiveScheduler• asynchronous equivalent of a reader/writer lock• Writer take priority over reader © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 32
    30. 30. WCF
    31. 31. WCF template support TAP The proxy default does generate TAP methods for each operation. At the service side WCF know how to translate methods that return Task<T>[ServiceContract]public interface IService1{ [OperationContract] Task<string> GetData(int value);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 34
    32. 32. WCF template support TAP The proxy default does generate TAP methods for each operation. At the service side WCF know how to translate methods that return Task<T>[ServiceContract]public interface IService1{ [OperationContract] Task<string> GetData(int value);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 35
    33. 33. C# 5: async / await
    34. 34. async / await .NET 4.5 TPL Dataflow Linq .NET 4 TPL (TAP), IObservable .NET 3.5 Extension Method XxxSlim .NET 3 yield return .NET 2delegate Generics .NET 1 Thread, ThreadPool, (APM, EAP) IEnumerable 37
    35. 35. async / await .NET 4.5 TPL Dataflow .NET 4 TPL .NET 3.5 XxxSlim .NET 3 .NET 2.NET 1 Thread, ThreadPool, (APM, EAP) 38
    36. 36. • async – mark the method as a-sync• await – delegate the execution to a call back thread markerstatic async Task Execute(){ delegate the execution to a Console.WriteLine(“run on calling thread”); call back thread await Task.Factory.StartNew(() => Thread.Sleep(1000)); Console.WriteLine(“run on callback thread”);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 39
    37. 37. • async – mark the method as a-sync• await – delegate the execution to a call back threadstatic async Task Execute(){ delegate the execution to a Console.WriteLine(“run on calling thread”); Thread Id = 1 call back thread await Task.Factory.StartNew(() => Thread.Sleep(1000)); Thread Id = 2 Console.WriteLine(“run on callback thread”); Thread Id = 3} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 40
    38. 38. static async Task Execute(){ Console.WriteLine(“run on calling thread”); await Task.Factory.StartNew(() => Thread.Sleep(1000)); Console.WriteLine(“run on callback thread”);}static Task Execute(){ Console.WriteLine(“run on calling thread”); Task t = Task.Factory.StartNew(() => Thread.Sleep(1000)); return t.ContinueWith (tsk => { Console.WriteLine(“run on callback thread”); });} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 41
    39. 39. static async Task Execute() Thread Id = 1{ Console.WriteLine(“run on calling thread”); await Task.Factory.StartNew(() => Thread.Sleep(1000)); Thread Id = 2 Console.WriteLine(“run on callback thread”);} Thread Id = 3static async Task Execute(){ Thread Id = 1 Console.WriteLine(“run on calling thread”); Task t = Task.Factory.StartNew(() => Thread.Sleep(1000)); Thread Id = 2 return t.ContinueWith (tsk => { Console.WriteLine(“run on callback thread”); Thread Id = 3 });} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 42
    40. 40. static async Task Execute(){ Console.WriteLine(“run on calling thread”); DateTime result = await Task.Factory.StartNew(() => DateTime.Now ); Console.WriteLine(result );}static Task Execute(){ Console.WriteLine(“run on calling thread”); Task<DateTime> t = Task.Factory.StartNew(() => DateTime.Now); return t.ContinueWith (tsk => { DateTime result = tsk.Result; Console.WriteLine(result ); });} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 43
    41. 41. • How do we handle Exception in Tpl?static void Execute(){ Console.WriteLine("run on calling thread"); … var tsk = Task.Factory.StartNew(() => …);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 44
    42. 42. • How do we handle Exception in Tpl?static void Execute(){ try { Console.WriteLine("run on calling thread"); … var tsk = Task.Factory.StartNew(() => …); } catch (Exception ex) { … }} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 45
    43. 43. • How do we handle Exception in Tpl?static void Execute(){ try { Console.WriteLine("run on calling thread"); … var tsk = Task.Factory.StartNew(() => …); } What about async catch (Exception ex) { … } operations} Handle synchronous code © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 46
    44. 44. • How do we handle Exception in Tpl?static void Execute(){ try { Console.WriteLine("run on calling thread"); … var tsk = Task.Factory.StartNew(() => {}); tsk.ContinueWith(t => Logger.Write(t.Exception), TaskContinuationOptions.OnlyOnFaulted ); } catch (Exception ex) { Logger.Write(ex); }} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 47
    45. 45. • How do we handle Exception using Async?static async Task Execute(){ try { Console.WriteLine(“run on calling thread”); await Task.Run(() => …); // working on other thread Console.WriteLine(“run on callback thread”); } catch (Exception ex) {…}} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 48
    46. 46. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 49
    47. 47. how long does it take to execute the method?private static async Task Execute(){ var sw = Stopwatch.StartNew(); await Task.Delay(1000); await Task.Delay(1000); Console.WriteLine("Async: {0:N3}", sw.Elapsed.TotalSeconds);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 50
    48. 48. await Task.Delay(1000); await Task.Delay(1000);Task t = Task.Delay(1000);t.ContinueWith (t1 =>{ Task t2 = Task.Delay(1000); t2.ContinueWith (t4 => …);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 51
    49. 49. how long does it take to execute the method?static async Task Execute(){ var sw = Stopwatch.StartNew(); Task t1 = Task.Delay(1000); Task t2 = Task.Delay(1000); await t1; await t2; Console.WriteLine("Async: {0:N3}", sw.Elapsed.TotalSeconds);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 52
    50. 50. http://blogs.msdn.com/b/pfxteam/archive/2011/09/28/10217876.aspxstatic async Task Execute(){ var sw = Stopwatch.StartNew(); Task t1 = Task.Delay(1000); Task t2 = Task.Delay(1000); await Task.WhenAll(t1, t2); Console.WriteLine("Async: {0:N3}", sw.Elapsed.TotalSeconds);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 53
    51. 51. Task t1 = Task.Delay(1000);Task t2 = Task.Delay(1000);await Task.WhenAll (t1, t2);Task t1 = Task.Delay(1000);Task t2 = Task.Delay(1000);Task[] arr = new Task[]{t1, t2};Task.Factory.ContinueWhenAll (arr, tsks => {…}); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 54
    52. 52. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 55
    53. 53. private static async Task Execute(){ var sw = Stopwatch.StartNew(); for (int i = 0; i < 10; i++) { await Task.Delay(100); } Console.WriteLine("Async: {0:N3}", sw.Elapsed.TotalSeconds);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 56
    54. 54. private static async Task Execute( int n ){ var sw = Stopwatch.StartNew(); for (int i = 0; i < n; i++) { await Task.Delay(100); } Console.WriteLine("Async: {0:N3}", sw.Elapsed.TotalSeconds);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 57
    55. 55. • Using a-sync syntax from UI thread will execute the operation on other thread and sync back to the UI thread after the await keyword.public partial class MainWindow : Window{ private async Task GetData() { Data = "Waiting"; // update the property on the UI thread string text = await Task.Factory.StartNew(() => { Thread.Sleep(5000); return "Hellow world"; }); Data = text; // update the property on the UI thread } public string Data { get; set; } // bound to UI element} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 58
    56. 56. Using a-sync syntax from UI thread will execute the operation on other thread and sync back to the UI thread after the await keyword.public partial class MainWindow : Window{ private async Task GetData() UI Thread { Data = "Waiting"; // update the property on the UI thread string text = await Task.Factory.StartNew(() => { Thread.Sleep(5000); return "Hellow world"; }); Data = text; // update the property on the UI thread } public string Data { get; set; } // bound to UI element} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 59
    57. 57. Using a-sync syntax from UI thread will execute the operation on other thread and sync back to the UI thread after the await keyword.public partial class MainWindow : Window{ private async Task GetData() { Data = "Waiting"; // update the property on the UI thread string text = await Task.Factory.StartNew(() => Thread Pool { Thread.Sleep(5000); return "Hellow world"; }); Data = text; // update the property on the UI thread } public string Data { get; set; } // bound to UI element} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 60
    58. 58. • Using a-sync syntax from UI thread will execute the operation on other thread and sync back to the UI thread after the await keyword.public partial class MainWindow : Window{ private async Task GetData() { Data = "Waiting"; // update the property on the UI thread string text = await Task.Factory.StartNew(() => { Thread.Sleep(5000); UI Thread return "Hellow world"; }); Data = text; // update the property on the UI thread } public string Data { get; set; } // bound to UI element} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 61
    59. 59. • Code reviewusing (var scope = new MyDisposable()){ … Task.Run(() => scope.Execute()); …} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 63
    60. 60. • the Dispose will be call on the completion thread.using (var scope = new MyDisposable()){ … await Task. Run(() => scope.Execute()); …} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 64
    61. 61. • the Dispose will be call on the completion thread.using (var scope = new MyDisposable()){ … await Task. Run(() => scope.Execute()); …} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 65
    62. 62. • Await can apply Lambda expressionFunc<Task> f = async () => { Console.WriteLine(“Synchronously"); await Task.Delay(1000); Console.WriteLine(“Parallel callback"); };f(); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 67
    63. 63. • await cannot be declare in: – constructor – lock – catch or finally – unsafe block © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 79
    64. 64. Reactive Extension (Rx)
    65. 65. async / await .NET 4.5 TPL Dataflow .NET 4 TPL (TAP), IObservable .NET 3.5 XxxSlim .NET 3 .NET 2 Generics.NET 1 Thread, ThreadPool, (APM, EAP) IEnumerable 85
    66. 66. Rx (Reactive Extension) is a library for composing asynchronous and event-based programs using observable.Rx = Push standard over Producer / Consumer (Pub /Sub) Pattern. Handling the continuation of event stream 86
    67. 67. Rx (Reactive Extension) is a library for composing asynchronous and event-based programs using observable.Rx = Push standard over Producer / Consumer (Pub /Sub) Pattern. SubscribeObservable Observer(Producer) (Consumer) Handling the continuation of event stream 87
    68. 68. Does it sound familiar? event EventHandler Click; Click += (s,e) => {…};Rx = Push standard over Producer / Consumer (Pub /Sub) Pattern. SubscribeObservable Observer(Producer) (Consumer) Handling the continuation of event stream 88
    69. 69. Rx = Linq to Events stream. - Event cant be composed into LINQ query. - Event cant be stored in variables and data structures. - Event cant be pass as parameter. from dress in producer where color == blue && IsReasonable(dress.price) select dress 89
    70. 70. • Concept• Where to use• How to use 90
    71. 71. GPS Accelerometer Stock TickersSocial Media Server Management UI OData © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 91
    72. 72. NextThread 1 Thread 2 ValueNext 1 Next 2Current Current 2 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 93
    73. 73. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 94
    74. 74. How can we reduce the Azure request costfor Search (send request only if the userinput is idle for 0.5 seconds) © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 95
    75. 75. • Google “Rx msdn” http://msdn.microsoft.com/en-us/data/gg577609 https://rx.codeplex.com• Platform: – NET 3.5 SP1 – .NET 4 – .NET 4.5 – WinRT / WinStore – Silverlight 3, 4 – Windows Phone – XBox and Zune via XNA – JavaScript © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 96
    76. 76. • Google “Rx msdn” http://msdn.microsoft.com/en-us/data/gg577609 https://rx.codeplex.com• NuGet – Rx-Main – Rx-Wpf – Rx-Testing – Rx-Silverlight – Rx-Xaml – Rx-WinForms – Rx-Providers © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 97
    77. 77. Producerpublic interface IObservable <T>{ IDisposable Subscribe (IObserver<T> observer);} IEnumerable<out T> { Consumer IEnumerator<T> GetEnumerator(); } IEnumerator<out T> : IDisposable, IEnumeratorpublic interface IObserver<T> {{ bool MoveNext(); void OnCompleted (); T Current { get; } void OnError (Exception exception); void Reset(); void OnNext (T value); }} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 98
    78. 78. Producerpublic interface IObservable <T>{ IDisposable Subscribe (IObserver<T> observer);} IEnumerable<out T> { Consumer IEnumerator<T> GetEnumerator(); } IEnumerator<out T> : IDisposable, IEnumeratorpublic interface IObserver<T> {{ bool MoveNext(); void OnCompleted (); T Current { get; } void OnError (Exception exception); void Reset(); void OnNext (T value); }} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 99
    79. 79. Producerpublic interface IObservable <T>{ IDisposable Subscribe (IObserver<T> observer);} IEnumerable<out T> { Consumer IEnumerator<T> GetEnumerator(); }public interface IObserver<T> IEnumerator<out T> Complete / Error : IDisposable, IEnumerator {{ bool MoveNext(); // throw exception void OnCompleted (); T Current { get; } void OnError (Exception exception); } void OnNext (T value);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 100
    80. 80. Producerpublic interface IObservable <T>{ IDisposable Subscribe (IObserver<T> observer);} IEnumerable<out T> { Consumer IEnumerator<T> GetEnumerator(); }public interface IObserver<T> IEnumerator<out T> : IDisposable, IEnumerator {{ bool MoveNext(); void OnCompleted (); T Current { get; } void OnError (Exception exception); } void OnNext (T value);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 101
    81. 81. Producerpublic interface IObservable <T>{ IDisposable Subscribe (IObserver<T> observer);} IEnumerable<out T> { Consumer IEnumerator<T> GetEnumerator(); }public interface IObserver<T> IEnumerator<out T> : IDisposable, IEnumerator {{ bool MoveNext(); void OnCompleted (); T Current { get; } void OnError (Exception exception); } void OnNext (T value);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 102
    82. 82. How do you unsubscribe from: Producer this.Click += (s,e) => {…};public interface IObservable <T>{ IDisposable Subscribe (IObserver<T> observer);} IEnumerable<out T> { Consumer IEnumerator<T> GetEnumerator(); }public interface IObserver<T> IEnumerator<out T> : IDisposable, IEnumerator {{ bool MoveNext(); void OnCompleted (); T Current { get; } void OnError (Exception exception); } void OnNext (T value);} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 103
    83. 83. static void Main(string[] args){ IObserver<int> c1 = new Consumer(0); IObserver<int> c2 = new Consumer(2); IObservable<int> p = new Producer(); IDisposable c1Unsubscribe = p.Subscribe(c1); Thread.Sleep(2000); IDisposable c2Unsubscribe = p.Subscribe(c2); Thread.Sleep(1000); c1Unsubscribe.Dispose(); // unsubscribe Console.ReadKey();} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 106
    84. 84. IDisposable unsubscribe = foo.Subscribe (value => Console.WriteLine(value));The following extension method is available for subscription:• Action<TSource> onNext• Action<TSource> onNext, Action onCompleted• Action<TSource> onNext, Action<Exception> onError• Action<TSource> onNext, Action<Exception> onError, Action onCompletedIDisposable unsubscribe = foo.Subscribe( value => Console.WriteLine(value), exc => Console.WriteLine(exc.Message)); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 107
    85. 85. static void Main(string[] args){ IObserver<int> c2 = new Consumer(2); IObservable<int> p = new Producer(); IDisposable c1Unsubscribe = p.Subscribe(item => Console.WriteLine(item)); IDisposable c2Unsubscribe = p.Subscribe(c2); Thread.Sleep(1000); c1Unsubscribe.Dispose(); // unsubscribe} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 108
    86. 86. • The marble diagram is the formal way of presenting the observable stream on next on next on next complete on next on next on error © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 111
    87. 87. • The marble diagram is the formal way of presenting the observable stream © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 112
    88. 88. var timeStream = Observable.Interval (TimeSpan.FromSeconds(1));var negativeTimeStream = from value in timeStream select -value; 1 2 3 value => -value -1 -2 -3 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 113
    89. 89. var timeStream = Observable.Interval (TimeSpan.FromSeconds(1));var negativeTimeStream = from value in timeStream select -value; 1 2 3 value => -value -1 -2 -3 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 114
    90. 90. var evenTimeStream = from value in timeStream where value % 2 == 0 select value; 1 2 3 4 5 6 2 4 6 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 115
    91. 91. 1 Sec 1 Sec 1 Sec 1 Sec• Create time base stream – Observable.Interval(…); – Observable.Timer(…); 2 Sec 1 Sec 1 Sec• Create immediate values – Observable.Range(…); – Observable.Return(..);• Create custom – Observable.Create(…) – Observable.Generate(…) © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 116
    92. 92. • Visual Rx is a free open source tool, which enable monitoring a real-time Rx datum stream.• http://visualrx.codeplex.com/ © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 117
    93. 93. • Retry: re-subscribe on error• Catch: subscribe fallback stream on error• Finally: finalization action © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 121
    94. 94. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 122
    95. 95. If a tree falls in a forest and no one is around to hear it,does it make a sound?if it did make a sound when nobody observed it,we should mark it as hot,otherwise it should be marked as cold. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 123
    96. 96. Hot observable streams are streams that are active regardless ofwhether or notthey are being observed.such stream are the Sensors (accelerometer) ,Stock Exchange orMouse move event, .Cold observable streams are streams that are activated uponsubscription(in most cases a new stream will be activated for eachsubscription).for example Read resources (File, Network). © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 124
    97. 97. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 125
    98. 98. Subject is great for propagation strategy Can merge multiple subscribers into single stream Very useful for custom channeling operators Hot stream.ISubject<in TSource, out TResult> :IObserver<TSource>, IObservable<TResult> IObserver IObservable Subject 12 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 8
    99. 99. class Program { class Program { event Action<int> E; ISubject<int> S = new Subject<int>(); static void Main() { static void Main() { var p = new Program(); var p = new Program(); p.E += i => p.S.Subscribe(i => Console.Write(i); Console.Write(i)); p.E(1); p.S.OnNext(1); p.E(2); p.S.OnNext(2); p.E(3); p.S.OnNext(3); } }} } © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 129
    100. 100. class Program { class Program { event Action<int> E; ISubject<int> S = new Subject<int>(); static void Main() { static void Main() { var p = new Program(); var p = new Program(); p.E += i => p.S.Subscribe(i => Console.Write(i); Console.Write(i)); p.E(1); p.S.OnNext(1); p.E(2); p.S.OnNext(2); p.E(3); p.S.OnNext(3); } }} } © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 130
    101. 101. class Program { class Program { event Action<int> E; ISubject<int> S = new Subject<int>(); static void Main() { static void Main() { var p = new Program(); var p = new Program(); p.E += i => p.S.Subscribe(i => Console.Write(i); Console.Write(i)); p.E(1); p.S.OnNext(1); p.E(2); p.S.OnNext(2); p.E(3); p.S.OnNext(3); } }} } © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 131
    102. 102. class Program { class Program { event Action<int> E; ISubject<int> S = new Subject<int>(); static void Main() { static void Main() { var p = new Program(); var p = new Program(); p.E += i => p.S.Subscribe(i => Console.Write(i); Console.Write(i)); p.E(1); p.S.OnNext(1); p.E(2); p.S.OnNext(2); p.E(3); p.S.OnNext(3); } }} } © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 132
    103. 103. class Program { class Program { event Action<int> E; ISubject<int> S = new Subject<int>(); static void Main() { static void Main() { var p = new Program(); var p = new Program(); p.E += i => p.S.Subscribe(i => Console.Write(i); Console.Write(i)); p.E(1); p.S.OnNext(1); p.E(2); p.S.OnNext(2); p.E(3); p.S.OnNext(3); } }} } © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 133
    104. 104. class Program { class Program { event Action<int> E; ISubject<int> S = new Subject<int>(); static void Main() { static void Main() { var p = new Program(); var p = new Program(); p.E += i => p.S.Subscribe(i => Console.Write(i); Console.Write(i)); p.E(1); p.S.OnNext(1); p.E(2); p.S.OnNext(2); p.E(3); p.S.OnNext(3); } }} } © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 134
    105. 105. class Program { class Program { event Action<int> E; ISubject<int> S = new Subject<int>(); static void Main() { static void Main() { var p = new Program(); var p = new Program(); p.E += i => p.S.Subscribe(i => Console.Write(i); Console.Write(i)); p.E(1); p.S.OnNext(1); p.E(2); p.S.OnNext(2); p.E(3); p.S.OnNext(3); } }} } © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 135
    106. 106. class Program { class Program { event Action<int> E; ISubject<int> S = new Subject<int>(); static void Main() { static void Main() { var p = new Program(); var p = new Program(); p.E += i => p.S.Subscribe(i => Console.Write(i); Console.Write(i)); p.E(1); p.S.OnNext(1); p.E(2); p.S.OnNext(2); p.E(3); p.S.OnNext(3); } }} } © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 136
    107. 107. class Program { class Program { event Action<int> E; ISubject<int> S = new Subject<int>(); static void Main() { static void Main() { var p = new Program(); var p = new Program(); p.E += i => p.S.Subscribe(i => Console.Write(i); Console.Write(i)); p.E(1); p.S.OnNext(1); p.E(2); p.S.OnNext(2); p.E(3); p.S.OnNext(3); } }} } © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 137
    108. 108. Can’t pass as a parameter Separation of Concerns: or store in a data-structure can be split into IObservable / IObserverclass Program { class Program { event Action<int> E; ISubject<int> S = new Subject<int>(); static void Main() { Support extension static void Main() { var p = new Program(); query method and linq var p = new Program(); Can be disposed p.E += i => p.S.Subscribe(i => Support concurrency Console.Write(i); Console.Write(i)); through scheduler p.E(1); p.S.OnNext(1); p.E(2); p.S.OnNext(2); p.E(3); p.S.OnNext(3); } }} } © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 138
    109. 109. • Different strategies for combining the result of multiple streams together © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 140
    110. 110. the merge combinator is used to merge multiple IObservable streams into singleIObservable streams T1 T2 T3 B1 B2 B3 T1 B1 B2 T2 B3 T3 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 141
    111. 111. the merge combinator is used to merge multiple IObservable streams into singleIObservable streams var xs = Observable.Interval(TimeSpan.FromSeconds(1)); var ys = Observable.Interval(TimeSpan.FromSeconds(0.2)); var zs = Observable.Merge(xs, ys); T1 T2 T3 zs.Subscribe(item => Console.Write(item)); B1 B2 B3 T1 B1 B2 T2 B3 T3 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 142
    112. 112. the zip combinator is used to synchronize 2 IObservable streams into a singleIObservable stream S1 S2 S3 P1 P2 P3 S1 + P1 S2 + P2 S3 + P3 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 144
    113. 113. the zip combinator is used to synchronize 2 IObservable streams into a singleIObservable stream var xs = Observable.Interval(TimeSpan.FromSeconds(1)); var ys = Observable.Interval(TimeSpan.FromSeconds(1)); var zs = xs.Zip(ys, (x, y) => x + y)); S1 S2 S3 zs.Subscribe(item => Console.Write(item)); P1 P2 P3 S1 + P1 S2 + P2 S3 + P3 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 145
    114. 114. the zip combinator may not be the right choice for non-synchronized stream. S1 S2 S3 S4 S5 S6 S7 S8 P1 P2 P3 S1 + P1 S2 + P2 S3 + P3 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 146
    115. 115. the buffer with time operator buffers values that occur within specifictime windows, and then publishs the buffered values whenever the time periodends. 3 seconds 3 seconds 1 2 3 4 5 6 7 8 9 {1,2,3,4} {5,6,7,8} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 149
    116. 116. the buffer with time operator buffers values that occur within specifictime windows, and then publishs the buffered values whenever the time periodends. 3 seconds 3 seconds 1 2 3 4 5 6 7 8 9 IObservable<long> xs = ...; {1,2,3,4} {5,6,7,8} IObservable<IList<long>> xsBufferd = xs.Buffer (TimeSpan.FromSeconds(1)); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 150
    117. 117. Day 1 Day 2Credit: http://enumeratethis.com/2011/07/26/financial-charts-reactive-extensions/ © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 151
    118. 118. Day 1 Day 2 High H How does it affect the program? H GC Gen 2 Close and Large object heapOpen C O O L C Low L © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 152
    119. 119. the Window operator buffers values that occur within specifictime windows, and then publishes the buffered values whenever the timeperiod ends. 3 seconds 3 seconds 1 2 3 4 5 6 7 1 2 3 4 5 6 7 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 153
    120. 120. the Window operator buffers values that occur within specifictime windows, and then publishes the buffered values whenever the timeperiod ends. 3 seconds 3 seconds 1 2 3 4 5 6 7 1 2 3 4 5 6 7 IObservable<IObservable<long>> xsBufferd = xs.Window(4); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 154
    121. 121. Day 1 Day 2 H H CO O L C LIObservable<IObservable<long>> xsBufferd = xs.Window(TimeSpan.FromDays(1)); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 155
    122. 122. • Drag & drop © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 156
    123. 123. Data (What)• Scheduler – Data – Threads Time – Time (Virtual Time) (when) • Commit points Thread (How) Observable.Interval(TimeSpan.FromSeconds(1), TaskPoolScheduler.Default); © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 157
    124. 124. • NewThreadScheduler: new thread execution.• ThreadPoolScheduler: thread pool execution.• TaskPoolScheduler: TPL execution• Scheduler.Immediate: current thread execution.• Scheduler.CurrentThread: current thread via queue execution.• EventLoopScheduler: single thread execution.• DispatcherScheduler: WPF thread (System.Reactive.Windows.Threading.dll)• ControlScheduler: WinForm thread (System.Reactive.Windows.Forms.dll) © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 158
    125. 125. Forum activity, similar / overlapping Microsoft technologiesRX PLINQ / Parallel Extensions TPL DataFlowThreads: 1,251 Threads: 793 Threads: 59Messages: 7,787 Messages: 3,828 Messages: 264Users: 665 Users: 1,004 Users: 44General Discussions: 239 General Discussions: 39 General Discussions: 6Microsoft Robotics CCR Async CTP AxumThreads: 411 Threads: 325 Threads: 125Messages: 1,858 Messages: 1,409 Messages: 490Users: 590 Users: 366 Users: 104General Discussions: 49 General Discussions: 27 General Discussions: 19 Friday, November 25, 2011 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 168
    126. 126. Rx is a powerful Linq-able dataflow extensionRx applies to the producer / consumer (pub / sub)patternRx has lots of extension operatorsRx can compose different event streams intocombined stream functionalityRx rely on the scheduler which can be mocked fortesting and replay purposes 16 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 9
    127. 127. 17© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 0
    128. 128. http://blogs.microsoft.co.il/blogs/bnaya/Rx at DelLab: http://msdn.microsoft.com/en-us/devlabs/ee794896101 Rx Samples: http://rxwiki.wikidot.com/101samplesLee Campbell –http://leecampbell.blogspot.com/2010/05/intro-to-rx.htmlhttp://leecampbell.blogspot.com/search/label/Rx 17 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 1
    129. 129. TPL Dataflow
    130. 130. async / await .NET 4.5 TPL Dataflow .NET 4 TPL .NET 3.5 XxxSlim .NET 3 .NET 2.NET 1 Thread, ThreadPool, (APM, EAP) 173
    131. 131. • TPL Dataflow (TDF) is a higher-level abstraction over the TPL• It provides common solutions for common parallel patterns• Focuses on providing agent-based models, building blocks for message passing and buffering parallelization © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 174
    132. 132. • TPL Dataflow (TDF) evolved from: – AAL: Asynchronous Agents Library (native) – Axum: agent-based language that builds upon the architecture of the Web and principles of isolation – CCR: (Microsoft Robotics) Concurrency and Coordination Runtime © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 177
    133. 133. • High-throughput / low-latency.• Immense CPU and I/O traffic• TDF’s Building blocks can be used: – Standalone – In composition with others (enabling complex dataflow networks)• Efficient execution that avoids oversubscription. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 178
    134. 134. • Download .NET 4.5• Reference: System.Threading.Tasks.Dataflow.dll – NuGet: Tpl Dataflow or TDF• Main namespaces: – System.Threading.Tasks.Dataflow – System.Threading.Tasks © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 179
    135. 135. • DTF is based on 2 interfaces: – ISourceBlock<T> // producer – ITargetBlock<T> // consumer Link To ISourceBlock<T> ITargetBlock<T>• Each node in the dataflow may either ISourceBlock<T>, ITargetBlock<T> or both. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 180
    136. 136. • OfferMessage: • Accept or Deny the message • Calls back to methods on the source block • consumeToAccept – communication pattern direct vs. indirect consumeDataflowMessageStatus OfferMessage( DataflowMessageHeader messageHeader, TInput messageValue, // push ISourceBlock<TInput> source, // pull bool consumeToAccept); // false = push 18 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 3
    137. 137. OfferMessage communicates back to caller either by using thesource parameter or by the DataflowMessageStatus return value.• Accepted: accepted the message and take ownership.• Declined• Postponed: postponed the message for potential consumption at a later time. The target may consume the message later by using the source ConsumeMessage.• NotAvailable: failed to consume the message (the message is no longer available)• DecliningPermanently: declined the message and all future messages sent by the source. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 184
    138. 138. • Data-pulling APIs. interface IReceivableSourceBlock<TOutput> : ISourceBlock<TOutput>{ bool TryReceive(out TOutput item, Predicate<TOutput> filter); bool TryReceiveAll(out IList<TOutput> items);} 18 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 5
    139. 139. LinkTo -> OfferMessage Push: LinkToISourceBlock<T> ITargetBlock<T> Pull: TryReceive TryReceive 18 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 6
    140. 140. • TPL Dataflow provides built-in blocks for most common and needed scenarios• The built-in blocks fall into three categories: – Pure Buffering blocks – Execution blocks – Grouping blocks 19 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
    141. 141. • A propagator which provides a buffer for storing instances of T• Non broadcasting nature: – Push via LinkTo will offer a message to each target in turn, allowing only one to consume each – Pull via Receives will remove the received element from the buffer © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 196
    142. 142. private BufferBlock<int> _buffer = new BufferBlock<int>();private void Producer(){ for (int i = 0; i < 10; i++) { _buffer.Post(i); }}private void Consumer(){ while (true) { int item = _buffer.Receive(); Console.WriteLine(item); }} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 197
    143. 143. private BufferBlock<int> _buffer = new BufferBlock<int>();private void Producer(){ Does it sound familiar? for (int i = 0; i < 10; i++) { _buffer.Post(i); }}private void Consumer(){ while (true) { int item = _buffer.Receive(); Console.WriteLine(item); }} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 198
    144. 144. private BufferBlock<int> _buffer = new = new ConcurrentQueue<int>(); ConcurrentQueue<int> _buffer BufferBlock<int>();private void Producer(){ for (int i = 0; i < 10; i++) { _buffer.Enqueue(i); _buffer.Post(i); }} What’s happens when the queue is empty?private void Consumer(){ int item; while (true) { while (_buffer.TryDequeue(out item)) { int item = _buffer.Receive(); Console.WriteLine(item); Console.WriteLine(item); } }} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 199
    145. 145. private BlockingCollection<int>_buffer = new ConcurrentQueue<int>(); ConcurrentQueue<int> _buffer = new BlockingCollection<int>();private void Producer(){ for (int i = 0; i < 10; i++) { _buffer.Add(i); _buffer.Enqueue(i); } Which thread is running?} Is it a thread pool’s thread?private void Consumer(){ foreach(int item in _buffer.GetConsumingEnumerable()) { int item; while (_buffer.TryDequeue(out item)) { Console.WriteLine(item); } Console.WriteLine(item);} }} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 200
    146. 146. private BlockingCollection<int> _buffer = new BlockingCollection<int>();private void Producer(){ for (int i = 0; i < 10; i++) { Watch your memory graph _buffer.Add(i); when idle!!! } Real-life story: Blocking} Collectionprivate void Consumer(){ foreach(int item in _buffer.GetConsumingEnumerable()) { Parallel.ForEach(_buffer.GetConsumingEnumerable(), item => { Console.WriteLine(item); }} © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 201
    147. 147. var abConsumer1 = new ActionBlock<int>(i => …));var abConsumer2 = new ActionBlock<int>(i => …));var bufferBlock = new BufferBlock<int>();bufferBlock.LinkTo (abConsumer1); /* target */bufferBlock.LinkTo (abConsumer2); /* target */bufferBlock.Post(1);bufferBlock.Post(2); ActionBlock BufferBlock ActionBlock © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 202
    148. 148. How does it different from Rx?Rx = direct push (functional programming).TDF = push via buffering (agent base).In general it is a better together scenario. Target Block Buffe r Task © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 203
    149. 149. • ISourceBlock<T>.AsObservable()• ITargetBlock<T>.AsObserver()var bufferBlock = new BufferBlock<int>();var xs = from item in bufferBlock.AsObservable() group item % 10 into g select g; © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 204
    150. 150. var options = new ExecutionDataflowBlockOptions {BoundedCapacity = 1};var abConsumer1 = new ActionBlock<int>(i => …, options ));var abConsumer2 = new ActionBlock<int>(i => …, options ));var bufferBlock = new BufferBlock<int>();bufferBlock.LinkTo (abConsumer1); /* target */bufferBlock.LinkTo (abConsumer2); /* target */bufferBlock.Post(1); Target BlockbufferBlock.Post(2); BoundedCapacity = 1 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 205
    151. 151. • A propagator which provides a buffer for storing instances of T• Broadcasting nature: – Always keep the last item – All linked targets will be offered a copy of the data, the block’s constructor gets a Func<T,T> for the cloning operation. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 206
    152. 152. var abConsumer1 = new ActionBlock<int>(i => …));var abConsumer2 = new ActionBlock<int>(i => …));var broadcastBlock = new BroadcastBlock<int>(i => i.Clone());broadcastBlock.LinkTo (abConsumer1); /* broadcast target */broadcastBlock.LinkTo (abConsumer2); /* broadcast target */broadcastBlock.Post(1);broadcastBlock.Post(2); TargetBlock BroadcastBlock TargetBlock © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 207
    153. 153. • How to avoid over subscription?• How to tune the blocks execution?• Why does the BroadcastBlock is having an input buffer? © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 208
    154. 154. • Blocks signaturevar downloader = new ActionBlock<string>(async url =>{ byte [] imageData = await DownloadAsync(url); Process(imageData);});downloader.Post("http://msdn.com/concurrency ");downloader.Post("http://blogs.msdn.com/pfxteam"); 20 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 9
    155. 155. Action Broadcast4 3 2 1 Action © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 210
    156. 156. Action Broadcast4 3 2 1 1 Action © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 211
    157. 157. Action 1Broadcast4 3 2 2 1 Action 1 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 212
    158. 158. Action 2 1Broadcast 4 3 3 2 3 Action 2 1 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 213
    159. 159. Action 3 2 1Broadcast 4 4 3 Action 2 1 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 214
    160. 160. Action 4 3 2Broadcast 4 Action 2 1 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 215
    161. 161. Action 4 3Broadcast 4 Action 2 1 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 216
    162. 162. • Can we avoid Task starvation? MaxMessagesPerTask © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 217
    163. 163. Action Broadcast4 3 2 1 1 Action © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 218
    164. 164. Action 1Broadcast4 3 2 2 1 Action 1 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 219
    165. 165. Action 2 1Broadcast 4 3 2 3 Action 2 1 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 220
    166. 166. Google: Bnaya Export Web Crawlerhttp://blogs.microsoft.co.il/blogs/bnaya/archive/2012/01/28/tpl-dataflow-walkthrough-part-5.aspx © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 221
    167. 167. 22© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 2
    168. 168. High level abstraction. Producer / Consumer = ISourceBlock / ITargetBlock Greedy vs. Non-Greedy Makes no built-in guarantees of isolation. Introduction to TPL Dataflow by Stephen Toub, Microsoft (part of the async CTP)http://blogs.microsoft.co.il/blogs/bnaya/ 22 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 3

    ×