From 4ad90cecc6c869a9c562d7b9366bff5661d9bb71 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 11 Oct 2020 11:48:42 +0300 Subject: [PATCH] Fix Client close waiting in C# example. GitOrigin-RevId: 411fb84b3f7b4840381962db4e761a04db464182 --- example/csharp/TdExample.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/example/csharp/TdExample.cs b/example/csharp/TdExample.cs index eb5c0c0f5..7c5b4d255 100644 --- a/example/csharp/TdExample.cs +++ b/example/csharp/TdExample.cs @@ -23,7 +23,8 @@ namespace TdExample private static TdApi.AuthorizationState _authorizationState = null; private static volatile bool _haveAuthorization = false; - private static volatile bool _quiting = false; + private static volatile bool _needQuit = false; + private static volatile bool _canQuit = false; private static volatile AutoResetEvent _gotAuthorization = new AutoResetEvent(false); @@ -133,9 +134,11 @@ namespace TdExample { Print("Closed"); _client.Dispose(); // _client is closed and native resources can be disposed now - if (!_quiting) + if (!_needQuit) { _client = CreateTdClient(); // recreate _client after previous has closed + } else { + _canQuit = true; } } else @@ -187,7 +190,7 @@ namespace TdExample _client.Send(new TdApi.Close(), _defaultHandler); break; case "q": - _quiting = true; + _needQuit = true; _haveAuthorization = false; _client.Send(new TdApi.Close(), _defaultHandler); break; @@ -228,7 +231,7 @@ namespace TdExample _defaultHandler.OnResult(Td.Client.Execute(new TdApi.GetTextEntities("@telegram /test_command https://telegram.org telegram.me @gif @test"))); // main loop - while (!_quiting) + while (!_needQuit) { // await authorization _gotAuthorization.Reset(); @@ -240,6 +243,9 @@ namespace TdExample GetCommand(); } } + while (!_canQuit) { + Thread.Sleep(1); + } } private class DefaultHandler : Td.ClientResultHandler