Close the ClientSample app when connection closed (#805)
This commit is contained in:
parent
7418785a5e
commit
ec18f7a1cb
|
|
@ -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)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue