Skip to content

Commit 477983f

Browse files
committed
update examples to use new API
1 parent 1216f1d commit 477983f

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

examples/Smdn.Net.MuninNode/local-node-uptime/Program.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,23 @@
7171
args.Cancel = true;
7272
};
7373

74-
// Start node and accept connections.
75-
node.Start();
76-
77-
await node.AcceptAsync(throwIfCancellationRequested: false, cts.Token);
78-
79-
// When the AcceptAsync method finishes processing one connection,
74+
// Start the node and accept connections.
75+
//
76+
// When the RunAsync method finishes processing one connection,
8077
// it immediately waits for the next connection.
8178
// Therefore, the method will not return until the cancellation is
8279
// requested by CancellationToken.
83-
//
84-
// By specifying false to the throwIfCancellationRequested, you can
85-
// let the AcceptAsync method to return without throwing
86-
// OperationCanceledException when a cancellation is requested.
80+
try {
81+
await node.RunAsync(cts.Token);
82+
}
83+
catch (OperationCanceledException ex) when (ex.CancellationToken == cts.Token) {
84+
// CancellationToken is triggered
85+
Console.WriteLine("stopped");
86+
}
8787

88-
Console.WriteLine("stopped");
88+
// Instead of RunAsync(), StartAsync() and AcceptAsync() can be called
89+
// separately to start the node.
90+
//
91+
// await node.StartAsync(cts.Token);
92+
// await node.AcceptAsync(throwIfCancellationRequested: false, cts.Token);
93+
// Console.WriteLine("stopped");

examples/Smdn.Net.MuninNode/multiple-fields/Program.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@
5959
args.Cancel = true;
6060
};
6161

62-
node.Start();
63-
64-
await node.AcceptAsync(throwIfCancellationRequested: false, cts.Token);
65-
66-
Console.WriteLine("stopped");
62+
try {
63+
await node.RunAsync(cts.Token);
64+
}
65+
catch (OperationCanceledException ex) when (ex.CancellationToken == cts.Token) {
66+
Console.WriteLine("stopped");
67+
}

0 commit comments

Comments
 (0)