English 中文(简体)
Threading / Linq Class list Issue
原标题:

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.

问题回答

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...





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签