Question

Argh! I'm back, guys! I hate having to bother others with my issues, but I've been working on this for like 3 days now.

Using the Chat Application example, I molded it into my game. The client and server connect appropriately but... My client is having some trouble. :/

    public static void appendText(RichTextBox box, string line)
    {
        if (box == null || box.IsDisposed)
            return;
        //try
        //{
            box.AppendText(line + Environment.NewLine);
            ScrollRichTextBox(box);
        //}
        //catch
        //{
        //    Program.debug.print("Something went wrong.");
        //}
    }

The AppendText line keeps throwing an exception (InvalidOperationException). I commented out the try-catch, hoping the compiler would give me more advice on what's wrong and maybe how to fix it, but I get no where with its help.

In the examples, I can run that code without getting this error. I don't know where I went wrong here.

Oh, AppendText is called by...

    public static void GotMessage(object peer)
    {
        NetIncomingMessage im;
        while ((im = s_client.ReadMessage()) != null)
        {
            // handle incoming message
            switch (im.MessageType)
            {
                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.ErrorMessage:
                case NetIncomingMessageType.WarningMessage:
                case NetIncomingMessageType.VerboseDebugMessage:
                    string text = im.ReadString();
                    //TextControl.appendText(menuWindow.richTextBoxStatus, text);
                    Program.printStatus(text);
                    break;

                case NetIncomingMessageType.StatusChanged:
                    NetConnectionStatus status = (NetConnectionStatus)im.ReadByte();

                    /*if (status == NetConnectionStatus.Connected)
                        s_form.EnableInput();
                    else
                        s_form.DisableInput();
                    */

                    //if (status == NetConnectionStatus.Disconnected)
                        //s_form.button2.Text = "Connect";

                    string reason = im.ReadString();
                    Program.printStatus(status.ToString() + ": " + reason);
                    break;

                case NetIncomingMessageType.Data:
                    // incoming packet from the server, handle data
                    int size = im.ReadInt32();
                    byte[] bData = im.ReadBytes(size);

                    string data = Encoding.Unicode.GetString(bData);

                    Program.debug.print(data);
                    Program.printToChat(data);
                    handleData(data);

                    break;

                default:
                    Program.printStatus("Unhandled type: " + im.MessageType + " " + im.LengthBytes + " bytes");
                    break;
            }   
        }
    }

where printToChat or printStatus are found. Those methods contain the calls to AppendText.

I tried to post to the Lidgren Google Group when the error first popped up, but I haven't gotten a response back from them, so I'm hoping people here could have the information I seek. :)

I can provide more information and code if needed (as always, heh).

(I did Google how to make thread-safe calls to UI elements, but the code was just too confusing to comprehend. When I finally thought I implemented a solution, it just refused to work at all..)

Was it helpful?

Solution

Pfft, look at me, figuring things out.

I was able to successfully add delegate methods to my forms so that they can be access across threads. Or so I'm led to believe. The code now functions as intended.

I coooould add code, to show what I did. And I will if anyone needs help with this type of problem. I think I'm capable of explaining it, but MSDN works wonders.

As always, I just had to ask the question, and read comments to be led in the right direction.

I really need to stop asking questions I end up answering days later. :/

I'm sooo sorry. I feel really bad for taking up space and wasting time.

edit: I've also written a very clever way of circumventing needing to use delegates. However, it requires the use of a timer, or a loop that also repaints the windows form every now and then (I've used a timer).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top