tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.11k stars 1.44k forks source link

Unable to run a thread twice, false positive error message. #2454

Closed wrharper-AASP closed 1 year ago

wrharper-AASP commented 1 year ago

created:

using Td = Telegram.Td;
using TdApi = Telegram.Td.Api;

namespace UNIT_Telegram.Telegram
{
    internal class TelegramBase
    {
        static internal string UserID;
        static internal string BotDisplayName;
        static internal string BotUserName;
        static internal string DataID;

        static internal Td.Client client = null;
        /*#pragma warning disable IDE0044 // Add readonly modifier
        static internal volatile bool EndLoop = false;
        static internal volatile bool _haveAuthorization = false;
        static internal volatile bool _needQuit = false;
        static internal volatile bool _canQuit = false;
        static internal volatile AutoResetEvent _gotAuthorization = new(false);
        static internal volatile bool alreadyrunning = false;
        static internal volatile bool runonce = false;
        #pragma warning restore IDE0044 // Add readonly modifier*/
        static internal bool EndLoop = false;
        static internal bool _haveAuthorization = false;
        static internal bool _needQuit = false;
        static internal bool _canQuit = false;
        static internal AutoResetEvent _gotAuthorization = new(false);
        static internal bool alreadyrunning = false;
        static internal bool runonce = false;

        static internal readonly DefaultHandler _defaultHandler = new();
        internal class DefaultHandler : Td.ClientResultHandler
        {
#pragma warning disable IDE0052 // Remove unread private members
            TelegramAdminPanel form;
#pragma warning restore IDE0052 // Remove unread private members

            internal void Load(TelegramAdminPanel form)
            {
                this.form = form;
            }

            void Td.ClientResultHandler.OnResult(TdApi.BaseObject baseObject)
            {
                //CrossThreadManagement.UpdateRichTextboxText(form.OutputRT, baseObject.GetType().ToString());
                //CrossThreadManagement.UpdateRichTextboxText(form.OutputRT, baseObject.ToString());
            }
        }

        static internal void CloseConnection()
        {
            _needQuit = true;
            _haveAuthorization = false;
            client.Send(new TdApi.Close(), _defaultHandler);
        }
    }
}

Call it with:

public void CreateTelegramThread()
        {
            TelegramBase._needQuit = false;
            TelegramBase.EndLoop = false;
            TelegramBase.alreadyrunning = false;
            TelegramBase.runonce = false;

            TelegramBase._defaultHandler.Load(this);
            // disable TDLib log
            Td.Client.Execute(new TdApi.SetLogVerbosityLevel(0));
            if (Td.Client.Execute(new TdApi.SetLogStream(new TdApi.LogStreamDefault())) is TdApi.Error)
            //if (Td.Client.Execute(new TdApi.SetLogStream(new TdApi.LogStreamFile("tdlib.log", 1 << 27, false))) is TdApi.Error)
            {
                CrossThreadManagement.UpdateRichTextboxText(OutputRT, "error with log stream");
                //throw new System.IO.IOException("Write access to the current directory is required");
                //Console.WriteLine("error with log stream");
            }

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

            // create Td.Client
            AuthRequest ar = new();
            TelegramBase.client = TelegramGlobals.CreateTdClient(this, ar);
        }

The crash is actually a false positive because it still runs, works, and even closes the connection properly with the error message up. How can I stop it from popping up after a 2nd run? image

levlam commented 1 year ago

A crash can't be "a false positive". You must not call Client.Run() more than once. It is supposed to be called on a dedicated thread, which will handle updates and responses from all TDLib instances.

wrharper-AASP commented 1 year ago

ok, it appeared that way since it worked every time anyway. I dropped the call down to this and did what you said, no errors now. thanks

public void CreateTelegramThread()
        {
            TelegramBase._needQuit = false;
            TelegramBase.EndLoop = false;
            TelegramBase.alreadyrunning = false;
            TelegramBase.runonce = false;

            // create Td.Client
            AuthRequest ar = new();
            TelegramBase.client = TelegramGlobals.CreateTdClient(this, ar);
        }