td icon indicating copy to clipboard operation
td copied to clipboard

It is possible to stop background client before finish process?

Open Feodoros opened this issue 3 years ago • 2 comments

Hello! In samples we do something like this:

new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = true;
                Td.Client.Run();
            }).Start();

This client will work from behind for the entire duration of the program. Until its completion. If I want to run it, download something, then stop, and run it again later, then how should I do it?

I don't want it to work from the back for the entire duration of the program. I want to stop it and run it as needed. If we call Td.Client.Run() again, we will get an error (Receive must not be called simultaneously from two different threads...).

Feodoros avatar Aug 22 '22 21:08 Feodoros

I use C# wrapper.

Feodoros avatar Aug 23 '22 15:08 Feodoros

In the latest TDLib versions method Client::Run is a static method and needs to be called only once before the first Client is created on a separate dedicated thread. The method processes updates and request responses from all TDLib instances and never returns. It doesn't consume any noticeable resources when there are no clients, so there are no reasons to stop the thread.

To close a client you need to use the method TdApi.Close and wait for TdApi.updateAuthorizationState with TdApi.AuthorizationStateClosed.

levlam avatar Aug 23 '22 15:08 levlam