线程/Linq 类列表问题

发布于 2024-08-10 14:10:38 字数 269 浏览 9 评论 0原文

我最近编写了一个非常复杂的多服务器 IRC 机器人,并且遇到了一个问题.. 我已经尽可能地精简了代码,可以在此处查看。

我的问题是,当我调用 Disconnect() 时,连接会失效,而不是断开连接并关闭给定的服务器。它还只是冻结调用类,而不是停止该类的正确实例。

任何类似问题的帮助或经验将不胜感激。 如果可以的话请附上代码。

I've written a very complex multi-server IRC bot recently, and have encountered an issue..
I have stripped down the code as much as I could which can be viewed here.

My issue is that when I call the Disconnect() the connection is voided instead of disconnecting and closing the given server. It also just freezes the calling class instead of stopping the correct instance of the Class.

Any help or experience with a similar issue would be greatly appreciated.
Please include code if you can.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在梵高的星空下 2024-08-17 14:10:38

首先,您需要添加一个中断,以便:

        foreach (Connection connect in connections)
        {
            if (searching == true)
            {
                if (connect.SERVERID == ServerID)
                {
                    connect.Stop();
                    isFound = true;
                    searching = false;
                    connections.Remove(connect);
                }
            }
        }

变为:

        foreach (Connection connect in connections)
        {
            if (connect.SERVERID == ServerID)
            {
                connect.Stop();
                isFound = true;
                connections.Remove(connect);
                break;
            }
        }

因为您正在修改集合,而不是使用 searching == true 子句。效率更高。

接下来,我会将您的线程运行更改为如下所示:

public void Run()
{
    bool WhileOn = true;
    NetworkStream stream;
    string inputLine;
    StreamReader reader;
    try
    {
        using(TcpClient irc = new TcpClient(SERVER, PORT))
        {
        ...
        }
    }
    catch (ThreadAbortException)
    {
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
        Thread.Sleep(5000);
    }
}

以便正确处理您的连接。您应该对您的流执行类似的操作。

最后,我会在设置的超时后在 Stop 函数中添加对线程的 Abort() 调用。但是,如果 TCP 套接字被操作系统阻止,我不确定中止调用是否会中断它......

First off, you need to add a break so that this:

        foreach (Connection connect in connections)
        {
            if (searching == true)
            {
                if (connect.SERVERID == ServerID)
                {
                    connect.Stop();
                    isFound = true;
                    searching = false;
                    connections.Remove(connect);
                }
            }
        }

Becomes:

        foreach (Connection connect in connections)
        {
            if (connect.SERVERID == ServerID)
            {
                connect.Stop();
                isFound = true;
                connections.Remove(connect);
                break;
            }
        }

Because you are modifying the collection, rather than using the searching == true clause. Much more efficient.

Next, I would change your thread run to look like this:

public void Run()
{
    bool WhileOn = true;
    NetworkStream stream;
    string inputLine;
    StreamReader reader;
    try
    {
        using(TcpClient irc = new TcpClient(SERVER, PORT))
        {
        ...
        }
    }
    catch (ThreadAbortException)
    {
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
        Thread.Sleep(5000);
    }
}

So that your connection gets properly disposed. You should do similarly for your stream.

And finally, I would add an Abort() call on your thread in the Stop function after a set timeout. If a TCP socket is blocked by the OS, however, I'm not sure if an abort call will interrupt it...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文