-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHost.cs
More file actions
144 lines (112 loc) · 3.79 KB
/
Copy pathHost.cs
File metadata and controls
144 lines (112 loc) · 3.79 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using StreamThreads;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static StreamThreads.StreamExtensions;
namespace StripeSample
{
internal class Host
{
private bool readyforwork;
public IEnumerable<StreamState> Flash()
{
var secondlightison = false;
while (true)
{
Console.Write("O");
yield return MakeNoise().Until(() => secondlightison).Background();
yield return Sleep(500);
Console.Write("_");
secondlightison = true;
yield return Sleep(500);
}
}
public IEnumerable<StreamState> MakeNoise()
{
while (true)
{
Console.Write("x");
yield return OK;
}
}
public IEnumerable<StreamState> StartupState()
{
yield return HandleFault().OnError();
yield return PrintDots(".").Until(() => readyforwork).Background();
yield return GetReady().Background(out var ready);
yield return ReturnNumbers().Background(out var ret);
yield return ready.Await();
Console.WriteLine($"GetReady returned {ready.Value}");
Console.WriteLine($"Random number {ret.Value}");
yield return WaitForever;
}
private IEnumerable<StreamState<int>> GetReady()
{
bool cancel = false;
yield return Background<int>(c => AnAsyncProcess(c), () => cancel);
yield return AnotherAsyncProcess().Background<int, int>(out var number);
yield return number.Await<int>();
Console.WriteLine($"AnotherAsyncProcess said : {number}");
yield return MakeStuff(2, '#').Await<int>();
yield return MakeStuff(22, '?').Until(() => readyforwork).Background<int>();
yield return MakeStuff(4, '@').Await<int>();
cancel = true;
yield return MakeStuff(6, '!').Await<int>();
readyforwork = true;
yield return Return(5000);
}
private IEnumerable<StreamState> MakeStuff(int strength, char product)
{
Console.WriteLine($"\r\nNow Making:'{product}'");
yield return PrintDots(product.ToString()).Background();
for (int i = 0; i < strength; i++)
{
yield return Sleep(300);
Console.Write(new String(product, i));
}
}
private IEnumerable<StreamState> PrintDots(string v)
{
while (true)
{
Console.Write(v);
yield return Sleep(new Random().Next(10, 100));
}
}
private IEnumerable<StreamState> HandleFault()
{
Console.Write("Crashed");
yield break;
}
private IEnumerable<StreamState> ReturnNumbers()
{
var rnd = new Random();
while (true)
{
yield return Return(rnd.Next(0, 100));
}
}
private void AnAsyncProcess(CancellationToken token)
{
var rnd = new Random();
for (int i = 0; i < 100000; i++)
{
Console.ForegroundColor = (ConsoleColor)rnd.Next(1, 15);
if (token.IsCancellationRequested)
{
Console.WriteLine("color change cancelled");
return;
}
}
}
private async Task<int> AnotherAsyncProcess()
{
await Task.Delay(2300);
Console.WriteLine("waited 2.3 secs");
return 5;
}
}
}