Weitere ähnliche Inhalte Ähnlich wie What's new in Visual Studio 2012 General (20) Kürzlich hochgeladen (20) What's new in Visual Studio 2012 General1. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
www.sela.co.il
6. • 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
7. • 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
8. • Continuations chain tasks one after another
var 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
10. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 10
11. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 11
12. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 12
13. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 13
14. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 14
15. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 15
16. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 16
18. 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
19. • 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
20. • 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
21. Operators that may cause sequential fallback in both .NET 4 and
.NET 4.5 are marked in blue, and operators that may cause fallback
in .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
22. • 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
23. • 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
24. • Define cancellation with timeout.
var ct = new CancellationTokenSource(TimeSpan.FromSeconds(30));
// or
var ct = new CancellationTokenSource();
ct.CancelAfter(TimeSpan.FromSeconds(30));
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 25
25. 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
26. • 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
27. • New overload that allow to pass object state into continuations
var 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
29. • 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
30. 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
31. • 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
33. 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
34. 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
36. async / await
.NET 4.5
TPL Dataflow
Linq .NET 4
TPL (TAP), IObservable
.NET 3.5
Extension Method
XxxSlim
.NET 3
yield return
.NET 2
delegate Generics
.NET 1
Thread, ThreadPool,
(APM, EAP)
IEnumerable 37
37. 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
38. • async – mark the method as a-sync
• await – delegate the execution to a call back thread
marker
static 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
39. • async – mark the method as a-sync
• await – delegate the execution to a call back thread
static 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
40. 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
41. 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 = 3
static 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
42. 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
43. • 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
44. • 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
45. • 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
46. • 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
47. • 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
48. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 49
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);
Console.WriteLine("Async: {0:N3}", sw.Elapsed.TotalSeconds);
}
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 50
50. 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
51. 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
53. 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
54. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 55
55. 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
56. 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
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.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
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() 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
59. 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
60. • 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
61. • Code review
using (var scope = new MyDisposable())
{
…
Task.Run(() => scope.Execute());
…
}
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 63
62. • 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
63. • 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
64. • Await can apply Lambda expression
Func<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
65. • 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
67. 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
68. 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
69. Rx (Reactive Extension) is a library for composing
asynchronous and event-based programs
using observable.
Rx = Push standard over Producer / Consumer (Pub /Sub) Pattern.
Subscribe
Observable Observer
(Producer) (Consumer)
Handling the continuation of event stream
87
70. Does it sound familiar? event EventHandler Click;
Click += (s,e) => {…};
Rx = Push standard over Producer / Consumer (Pub /Sub) Pattern.
Subscribe
Observable Observer
(Producer) (Consumer)
Handling the continuation of event stream
88
71. Rx = Linq to Events stream.
- Event can't be composed into LINQ query.
- Event can't be stored in variables and data structures.
- Event can't be pass as parameter.
from dress in producer
where color == blue &&
IsReasonable(dress.price)
select dress
89
73. GPS
Accelerometer Stock Tickers
Social Media
Server Management
UI
OData
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 91
74. Next
Thread 1 Thread 2 Value
Next 1
Next 2
Current Current 2
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 93
75. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 94
76. How can we reduce the Azure request cost
for Search (send request only if the user
input is idle for 0.5 seconds)
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 95
77. • 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
78. • 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
79. Producer
public interface IObservable <T>
{
IDisposable Subscribe (IObserver<T> observer);
}
IEnumerable<out T>
{
Consumer IEnumerator<T> GetEnumerator();
}
IEnumerator<out T> : IDisposable, IEnumerator
public 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
80. Producer
public interface IObservable <T>
{
IDisposable Subscribe (IObserver<T> observer);
}
IEnumerable<out T>
{
Consumer IEnumerator<T> GetEnumerator();
}
IEnumerator<out T> : IDisposable, IEnumerator
public 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
81. Producer
public 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
82. Producer
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 101
83. Producer
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 102
84. 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
85. 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
86. 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 onCompleted
IDisposable 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
87. 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
88. • 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
89. • 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
90. 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
91. 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
92. 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
93. 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
94. • 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
95. • 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
96. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 122
97. 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
98. Hot observable streams are streams that are active regardless of
whether or not
they are being observed.
such stream are the Sensors (accelerometer) ,Stock Exchange or
Mouse move event, .
Cold observable streams are streams that are activated upon
subscription
(in most cases a new stream will be activated for each
subscription).
for example Read resources (File, Network).
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 124
99. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 125
100. 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
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 129
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 130
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 131
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 132
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 133
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 134
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 135
108. 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
109. 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
110. Can’t pass as a parameter Separation of Concerns:
or store in a data-structure can be split into
IObservable / IObserver
class 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
111. • 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
112. the merge combinator is used to merge multiple IObservable streams into single
IObservable 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
113. the merge combinator is used to merge multiple IObservable streams into single
IObservable 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
114. the zip combinator is used to synchronize 2 IObservable streams into a single
IObservable 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
115. the zip combinator is used to synchronize 2 IObservable streams into a single
IObservable 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
116. 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
117. the buffer with time operator buffers values that occur within specific
time windows, and then publishs the buffered values whenever the time period
ends.
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
118. the buffer with time operator buffers values that occur within specific
time windows, and then publishs the buffered values whenever the time period
ends.
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
119. Day 1 Day 2
Credit: 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
120. Day 1 Day 2
High
H How does it affect the
program?
H GC Gen 2
Close
and Large object heap
Open C
O
O
L C
Low
L
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 152
121. the Window operator buffers values that occur within specific
time windows, and then publishes the buffered values whenever the time
period 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
122. the Window operator buffers values that occur within specific
time windows, and then publishes the buffered values whenever the time
period 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
123. Day 1 Day 2
H
H
C
O
O
L C
L
IObservable<IObservable<long>> xsBufferd =
xs.Window(TimeSpan.FromDays(1));
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 155
124. • Drag & drop
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 156
125. 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
126. • 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
127. Forum activity, similar / overlapping Microsoft technologies
RX PLINQ / Parallel Extensions TPL DataFlow
Threads: 1,251 Threads: 793 Threads: 59
Messages: 7,787 Messages: 3,828 Messages: 264
Users: 665 Users: 1,004 Users: 44
General Discussions: 239 General Discussions: 39 General Discussions: 6
Microsoft Robotics CCR Async CTP Axum
Threads: 411 Threads: 325 Threads: 125
Messages: 1,858 Messages: 1,409 Messages: 490
Users: 590 Users: 366 Users: 104
General 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
128. Rx is a powerful Linq-able dataflow extension
Rx applies to the producer / consumer (pub / sub)
pattern
Rx has lots of extension operators
Rx can compose different event streams into
combined stream functionality
Rx rely on the scheduler which can be mocked for
testing and replay purposes
16
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
9
129. 17
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
0
130. http://blogs.microsoft.co.il/blogs/bnaya/
Rx at DelLab: http://msdn.microsoft.com/en-
us/devlabs/ee794896
101 Rx Samples: http://rxwiki.wikidot.com/101samples
Lee Campbell –
http://leecampbell.blogspot.com/2010/05/intro-to-rx.html
http://leecampbell.blogspot.com/search/label/Rx
17
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
1
132. 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
133. • 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
134. • 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
135. • 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
136. • 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
137. • 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
138. • OfferMessage:
• Accept or Deny the message
• Calls back to methods on the source block
• consumeToAccept – communication pattern direct vs. indirect consume
DataflowMessageStatus 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
139. OfferMessage communicates back to caller either by using the
source 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
140. • 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
141. LinkTo -> OfferMessage
Push: LinkTo
ISourceBlock<T> ITargetBlock<T>
Pull: TryReceive
TryReceive
18
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
6
142. • 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
143. • 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
144. 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
145. 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
146. 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
147. 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
148. 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
} Collection
private 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
149. 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
150. 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
152. 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 Block
bufferBlock.Post(2);
BoundedCapacity = 1
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 205
153. • 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
154. 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
155. • 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
156. • Blocks signature
var 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
157. Action
Broadcast
4 3 2 1
Action
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 210
158. Action
Broadcast
4 3 2 1 1
Action
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 211
159. Action
1
Broadcast
4 3 2 2
1
Action
1
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 212
160. Action
2 1
Broadcast
4 3 3
2
3
Action
2 1
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 213
161. Action
3 2 1
Broadcast
4 4
3
Action
2 1
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 214
162. Action
4 3 2
Broadcast
4
Action
2 1
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 215
163. Action
4 3
Broadcast
4
Action
2 1
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 216
164. • Can we avoid Task starvation?
MaxMessagesPerTask
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 217
165. Action
Broadcast
4 3 2 1 1
Action
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 218
166. Action
1
Broadcast
4 3 2 2
1
Action
1
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 219
167. Action
2 1
Broadcast
4 3 2
3
Action
2 1
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel 220
168. Google: Bnaya Export Web Crawler
http://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
169. 22
© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
2
170. 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
Hinweis der Redaktion 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<int, int>(); varsw = Stopwatch.StartNew(); cd.TryAdd(42, 0); for (int i = 1; i < 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("Start");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, TaskContinuationOptions.LazyCancellation, TaskScheduler.Default); Task c = b.ContinueWith( t => Console.WriteLine("C"));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 => { Write();Task.Factory.StartNew(Write);Task.Factory.StartNew(() => 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 (_ => Environment.TickCount); varys = Observable.Zip(xs, xs, (l, r) => l == r); //varxs_ = xs.Publish(); //varys = Observable.Zip(xs_, xs_, (l, r) => l == r); //xs_.Connect(); //varxs_ = xs.Publish().RefCount(); //varys = Observable.Zip(xs_, xs_, (l, r) => l == r); //varys = xs.Publish(xs_ => // Observable.Zip(xs_, xs_, (l, r) => 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 & 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 & 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 & 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<T> and ITargetBlock<T> 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 & 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 -> download -> find links -> outputFilter the link if web page,Like to image download block if image