Close the ClientSample app when connection closed (#805)

This commit is contained in:
BrennanConroy 2017-08-31 15:31:10 -07:00 committed by GitHub
parent 7418785a5e
commit ec18f7a1cb
1 changed files with 16 additions and 1 deletions

View File

@ -51,9 +51,24 @@ namespace ClientSample
// Set up handler // Set up handler
connection.On<string>("Send", Console.WriteLine); connection.On<string>("Send", Console.WriteLine);
connection.Closed += e =>
{
Console.WriteLine("Connection closed.");
cts.Cancel();
return Task.CompletedTask;
};
var ctsTask = Task.Delay(-1, cts.Token);
while (!cts.Token.IsCancellationRequested) while (!cts.Token.IsCancellationRequested)
{ {
var line = await Task.Run(() => Console.ReadLine(), cts.Token); var completedTask = await Task.WhenAny(Task.Run(() => Console.ReadLine(), cts.Token), ctsTask);
if (completedTask == ctsTask)
{
break;
}
var line = await (Task<string>)completedTask;
if (line == null) if (line == null)
{ {