-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneralExample2.cs
More file actions
64 lines (44 loc) · 1.78 KB
/
Copy pathGeneralExample2.cs
File metadata and controls
64 lines (44 loc) · 1.78 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
using Kwerty.DviZe.Win.Wnf;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;
namespace ExampleApp1;
public class GeneralExample2(WnfClient wnfClient, ILogger<GeneralExample2> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Create new state.
var stateName = wnfClient.Create<MyStateData>(WnfLifetime.Temporary, WnfScope.User);
logger.LogInformation("Created state 0x{stateName:X}.", stateName);
// Subscribe to events.
var subscription = await wnfClient.SubscribeAsync<MyStateData>(stateName, evt =>
{
logger.LogInformation("[Event] Data changed: Val1={val1} Val2={val2}.", evt.Data.Val1, evt.Data.Val2);
}, stoppingToken);
// Update the state.
var val = new MyStateData
{
Val1 = true,
Val2 = 67,
};
wnfClient.Update(stateName, val);
logger.LogInformation("Updated state data: Val1={val1} Val2={val2}.", val.Val1, val.Val2);
// Query the state.
var result = wnfClient.Query<MyStateData>(stateName);
logger.LogInformation("Queried state data: Val1={val1} Val2={val2}.", result.Data.Val1, result.Data.Val2);
// Unsubscribe from events.
subscription.Dispose();
logger.LogInformation("Unsubscribed from events.");
// Delete the state.
// Note: This this will cause subscribers to receive an event with a zero-length buffer,
// which is why we unsubscribed first.
wnfClient.Delete(stateName);
logger.LogInformation("Deleted state.");
}
struct MyStateData
{
public bool Val1;
public int Val2;
}
}