Fix Client close waiting in C# example.

GitOrigin-RevId: 411fb84b3f7b4840381962db4e761a04db464182
This commit is contained in:
levlam 2020-10-11 11:48:42 +03:00
parent 29cd47f01a
commit 4ad90cecc6

View File

@ -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