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?