2013-04-17 36 views
0

我有一个使用smartirc4net库的IRC bot。我遇到了一个bot正在侦听命令的问题,并且我想让它运行的线程退出。 Listen()命令永远阻止。如果我使用ListenOnce(),我可以将呼叫放入While(!ShouldExit)循环中,但我必须等待机器人才能触发ListenOnce()smartirc4net永远听,不能退出线程

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

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

     irc.Disconnect(); 
    } 

作为变通,当机器人从父线程发出断开连接命令,它会发送本身就是一个信息:

/// <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"); 
    } 

这会触发ListenOnce()事件环回while循环,然后成功断开连接。 我正在接近这个不正确吗?有没有一种更清晰的方法让机器人立即断开连接?

回答

相关问题