c# - 阻塞监听防止断开连接

标签 c# multithreading blocking irc

问题概述:

我需要连接到 IRC 服务器。连接后,程序将向 channel 发送一条消息,并通过多条线路返回响应。我需要阅读这些行并将其存储在一个变量中以备后用。消息末尾的特殊字符 (]) 将定义多行消息的结尾。一旦我们收到这个字符,IRC session 应该断开并且处理应该继续。

情况:

我正在使用 Smartirc4net 库。调用 irc.Disconnect() 需要大约 40 秒来断开 session 。一旦我们收到 ] 字符, session 应该断开连接,Listen() 不应该阻塞,程序的其余部分应该继续运行。

研究:

我找到了这个:smartirc4net listens forever, can't exit thread ,我认为这可能是同一个问题,但是,我不确定我需要做什么来解决这个问题。

代码:

public class IrcCommunicator
    {
        public IrcClient irc = new IrcClient();

        string data;

        public string Data { get { return data; } }


        // this method we will use to analyse queries (also known as private messages)
        public void OnQueryMessage(object sender, IrcEventArgs e)
        {
            data += e.Data.Message;
            if (e.Data.Message.Contains("]"))
            {
                irc.Disconnect();  //THIS TAKES 40 SECONDS!!!

            }
        }

        public void RunCommand()
        {
            irc.OnQueryMessage += new IrcEventHandler(OnQueryMessage);

            string[] serverlist;
            serverlist = new string[] { "127.0.0.1" };
            int port = 6667;
            string channel = "#test";

            try
            {
                irc.Connect(serverlist, port);
            }
            catch (ConnectionException e)
            {
                // something went wrong, the reason will be shown
                System.Console.WriteLine("couldn't connect! Reason: " + e.Message);
            }

            try
            {
                // here we logon and register our nickname and so on 
                irc.Login("test", "test");
                // join the channel
                irc.RfcJoin(channel);
                irc.SendMessage(SendType.Message, "test", "!query");

                // here we tell the IRC API to go into a receive mode, all events
                // will be triggered by _this_ thread (main thread in this case)
                // Listen() blocks by default, you can also use ListenOnce() if you
                // need that does one IRC operation and then returns, so you need then 
                // an own loop 

                irc.Listen();

                // when Listen() returns our IRC session is over, to be sure we call
                // disconnect manually
                irc.Disconnect();
            }
            catch (Exception e)
            {
                // this should not happen by just in case we handle it nicely
                System.Console.WriteLine("Error occurred! Message: " + e.Message);
                System.Console.WriteLine("Exception: " + e.StackTrace);
            }
        }

    }




        IrcBot bot = new IrcBot();
        bot.RunCommand();

        ViewBag.IRC = bot.Data;

如你所见,一旦这个 感谢您花时间查看这段代码并阅读我的问题描述。如果您有任何想法或其他建议,请告诉我。

迈克

最佳答案

在 irc.Disconnect() 之前,通过在 OnQueryMessage() 中调用 RfcQuit(),我能够立即成功断开连接;

关于c# - 阻塞监听防止断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29990581/

相关文章:

multithreading - std::thread 连接中的竞争条件

ios - 如何在后台线程中解析?

Java 停止在输入流读取上阻塞的线程

web-applications - 当另一个用户正在编辑记录时阻止编辑该记录 - 如何操作?

Python:当陷入阻塞 raw_input 时如何退出 CLI?

c# - 从表达式中获取字符串属性名称

c# - 强制将 ASP.NET MVC3 和 Entity Framework 4 属性转换为大写

c# - 富文本框超链接

c# - 我应该在哪里/如何创建我的类的实例?

java - 如果 ReentrantLock 已锁定,则等待但不锁定该锁