-
Notifications
You must be signed in to change notification settings - Fork 0
/
SourceSamples.linq
87 lines (61 loc) · 2.21 KB
/
SourceSamples.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<Query Kind="Statements">
<Reference><ApplicationData>\LINQPad\Samples\Programming Reactive Extensions and LINQ\System.Reactive.dll</Reference>
<Namespace>System.Reactive</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
</Query>
/* Return 1:
*
* Can't get any simpler than this
*/
var input = Observable.Return(42);
input.Subscribe(x => Console.WriteLine("The number is {0}", x));
/* Return 2:
*
* Show that Return Completes after returning its one value
*/
// Materialize lets us see the OnComplete, which normally isn't part of the
// values returned in the stream.
var input = Observable.Return(42).Materialize();
input.Dump();
/* Return 3:
*
* Demonstrate that Return is a Cold Observable (i.e. that it produces a new
* stream of events every time someone subscribes to it)
*/
var input = Observable.Return(42);
input.Subscribe(x => Console.WriteLine("Subscription 1: {0}", x));
input.Subscribe(x => Console.WriteLine("Subscription 2: {0}", x));
/* Range 1
*
* Nothing particularly exciting here
*/
var input = Observable.Range(1, 100);
input.Sum().Subscribe(x => Console.WriteLine("The Sum is {0}", x));
/* Timer 1
*
* Demonstrate a simple use of Observable.Timer
*/
// We've specified that we want the timer to start *immediately*, that it should
// tick every second, and that we want the ticks to appear as Task<T>'s on other
// threads
var timer = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1.0), Scheduler.TaskPool);
timer.Take(5).Subscribe(x => Console.WriteLine(x % 2 == 0 ? "Tick" : "Tock"));
// Linqpad needs to wait while the tasks are running
Thread.Sleep(5500);
/* Start 1:
*
* Run an Action in the background and give us an Observable that represents the
* background task. Equivalent to (new Task(() => {...})).Start()
*
* This sample also demonstrates how to use First() to block on an Observable.
*/
var task = Observable.Start(() => {
Console.WriteLine("Hello World!");
// Do something very time-consuming here
Thread.Sleep(1000);
return;
});
// Wait until the task is completed
task.First();
Console.WriteLine("We're Finished!");
// vim: ts=4 sw=4 tw=80 et :