smartirc4net listens forever, can't exit thread

独自空忆成欢 提交于 2019-12-13 04:15:39

问题


I have a IRC bot that uses the smartirc4net library. I have run into an issue where the bot is listening for commands and I want the thread it is running on to exit. The Listen() command blocks forever. If I use ListenOnce() I can put the call inside of a While(!ShouldExit) loop, but I have to wait for the bot for something to trigger the ListenOnce().

    protected void irc_OnConnected(object sender, EventArgs e)
    {
        irc.Login(configuration.IRCNick, configuration.IRCNick);

        while (!_shouldDisconnect)
        {
            irc.ListenOnce();
        }

        irc.Disconnect();
    }

As a work around, when the bot is issued a disconnect command from the parent thread, it sends itself a message:

    /// <summary>
    /// Used by the parent thread to disconnect the bot
    /// </summary>
    /// <returns></returns>
    public void Disconnect()
    {
        _shouldDisconnect = true;

        irc.SendMessage(SendType.Message, irc.Nickname, "EXIT YOU STUPID BOT");
    }

This triggers the ListenOnce() event to loop back through the while loop, then successfully disconnect. Am I approaching this incorrectly? Is there a cleaner approach for getting the bot to disconnect immediately?


回答1:


Found that this is a bug in the latest code, confirmed with meebey over at https://github.com/meebey/SmartIrc4net/issues/4



来源:https://stackoverflow.com/questions/16064196/smartirc4net-listens-forever-cant-exit-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!