简体   繁体   English

Lidgren-InvalidOperationException未处理

[英]Lidgren - InvalidOperationException was unhandled

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). AppendText行不断抛出异常(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. 我注释掉了try-catch,希望编译器可以为我提供更多有关错误的内容以及可能的解决方法的建议,但是我在它的帮助下无处可寻。

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... 哦,AppendText被...调用

    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. 找到printToChat或printStatus的位置。 Those methods contain the calls to AppendText. 这些方法包含对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. 错误首次出现时,我曾尝试将邮件发布到Lidgren Google网上论坛,但我还没有得到他们的回复,因此我希望这里的人可以提供我所寻求的信息。 :) :)

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..) (我曾为Google做过如何对UI元素进行线程安全的调用,但是代码太让人迷惑而无法理解。当我终于以为自己实现了一个解决方案时,它根本拒绝工作。)

Pfft, look at me, figuring things out. Pfft,看着我,找出答案。

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. 我认为我有能力解释它,但是MSDN可以解决问题。

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). 但是,它需要使用计时器,或者不时循环重新绘制Windows窗体的循环(我使用了计时器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM