Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit ec18f7a

Browse files
Close the ClientSample app when connection closed (#805)
1 parent 7418785 commit ec18f7a

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

samples/ClientSample/HubSample.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,24 @@ public static async Task<int> ExecuteAsync(string baseUrl)
5151
// Set up handler
5252
connection.On<string>("Send", Console.WriteLine);
5353

54+
connection.Closed += e =>
55+
{
56+
Console.WriteLine("Connection closed.");
57+
cts.Cancel();
58+
return Task.CompletedTask;
59+
};
60+
61+
var ctsTask = Task.Delay(-1, cts.Token);
62+
5463
while (!cts.Token.IsCancellationRequested)
5564
{
56-
var line = await Task.Run(() => Console.ReadLine(), cts.Token);
65+
var completedTask = await Task.WhenAny(Task.Run(() => Console.ReadLine(), cts.Token), ctsTask);
66+
if (completedTask == ctsTask)
67+
{
68+
break;
69+
}
70+
71+
var line = await (Task<string>)completedTask;
5772

5873
if (line == null)
5974
{

0 commit comments

Comments
 (0)