-
Notifications
You must be signed in to change notification settings - Fork 35
/
CoreExample.razor
121 lines (101 loc) · 3.69 KB
/
CoreExample.razor
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
@inject IWorkerFactory workerFactory
@using BlazorWorker.Core.CoreInstanceService
@using BlazorWorker.Demo.Shared
@using BlazorWorker.Core
<div class="row">
<div class="col-5 col-xs-12">
<h1>Core .NET Worker threads with messaging.</h1>
Welcome to your new multithreaded app.
This example uses the Core module, which does not provide any serialization.<br />
This is useful for scenarios with simple API's (smaller download size), or for building a custom high-level API.
<br /><br />
Pi estimation demo. Specify number of iterations.<br />
<input type="text" @bind="piIterations" placeholder="estimation iterations" />
<br />
<progress max="@piIterations" value="@piProgress" /><br /><br />
<button disabled=@RunDisabled @onclick="OnClick" class="btn btn-primary">Run Test</button>
<button disabled="@disabled" @onclick="OnDispose" class="btn btn-secondary">Dispose Service</button>
<br />
<br />
<strong>Output:</strong>
<hr />
<pre>
@output
</pre>
</div>
<div class="col-7 col-xs-12">
<GithubSource RelativePath="Pages/CoreExample.razor" />
</div>
</div>
@code {
string RunDisabled => Running ? "disabled" : null;
bool Running = false;
int piIterations = 5_000_000;
int piProgress = 0;
string output;
IWorker worker;
ICoreInstanceService backgroundService;
IInstanceHandle handle;
string disabled = "disabled";
string rn = Environment.NewLine;
public async Task OnClick(EventArgs _)
{
output = "";
try
{
if (worker == null)
{
worker = await workerFactory.CreateAsync();
worker.IncomingMessage += this.OnWorkerMessage;
}
if (backgroundService == null)
{
disabled = null;
output = $"{rn}{LogDate()} Creating background service...";
StateHasChanged();
backgroundService = worker.CreateCoreInstanceService();
handle = await backgroundService.CreateInstance<CoreMathsService>(/*options =>
options.AddConventionalAssemblyOfService()
#if NET5_0_OR_GREATER
.AddAssemblies("System.Text.RegularExpressions.dll")
#endif
*/);
output += $"{rn}{LogDate()} Background service created.";
StateHasChanged();
}
output += $"{rn}{LogDate()} Calling EstimatePI({piIterations})...";
await worker.PostMessageAsync($"{nameof(MathsService.EstimatePI)}({piIterations})");
// Result is obtained in message listener "OnWorkerMessage"
}
catch (Exception e)
{
output = $"{rn}Error = {e}";
}
}
public void OnWorkerMessage(object sender, string message)
{
if (message.StartsWith($"{CoreMathsService.EventsPi}:"))
{
piProgress = int.Parse(message.Substring($"{CoreMathsService.EventsPi}:".Length).Trim());
StateHasChanged();
}
if (message.StartsWith($"{CoreMathsService.ResultMessage}:"))
{
var result = double.Parse(message.Substring($"{CoreMathsService.ResultMessage}:".Length).Trim());
output += $"{rn}{LogDate()} EstimatePI({piIterations}) = {result}";
StateHasChanged();
}
}
public async Task OnDispose()
{
await handle.DisposeAsync();
backgroundService = null;
await worker.DisposeAsync();
worker = null;
disabled = "disabled";
}
private string LogDate()
{
return DateTime.Now.ToString("HH:mm:ss:fff");
}
}