简体   繁体   English

调试Windows窗体应用程序

[英]Debugging a windows form application

Im making a chat program that saves the messages in files like pub0.zzc, all computers that are using it will be connected to the hard drive that these files are in, so its fine. 我正在制作一个聊天程序,将消息保存在pub0.zzc之类的文件中,所有使用该程序的计算机都将连接到这些文件所在的硬盘上,因此很好。 The method data.Chat.Read(MessageTypes type, string Channel) infinite loops through a try catch statement till it returns the messages. 方法data.Chat.Read(MessageTypes type,string Channel)无限循环通过try catch语句,直到返回消息。 I used this before and works perfectly. 我以前用过,效果很好。 But, my code was hard to manage so instead of just putting text boxes into the window and using the code each time, i created a user control (MessageViewer). 但是,我的代码很难管理,因此我创建了一个用户控件(MessageViewer),而不仅仅是将文本框放到窗口中并每次使用该代码。 It works fine, once again when I run it, BUT it freezes VS whenever I try to use the designer on the window housing the control. 它运行良好,再次运行时,但是每当我尝试在控件的窗口上使用设计器时,它就会冻结VS。 the probelm isnt the window because when i delete the control its fine. 该探针不是窗口,因为当我删除控件时,它很好。 I think the possible errors are at RefreshMessages() and the Refresher_Tick(...) 我认为可能的错误是在RefreshMessages()和Refresher_Tick(...)

Refresher.Stop() and .Start() is also not it, worked fine before Refresher.Stop()和.Start()也不是,之前工作正常

so here is the code: 所以这是代码:

    private void Refresher_Tick(object sender, EventArgs e)
    {
        Refresher.Stop();
        int RefreshRate = 4;
        bool Live = true;
        if (RefreshRateChoice == "Manual")
        {
            Live = false;
            RefreshRate = 1;
        }
        else if (RefreshRateChoice == "4 ( Default )")
        {
            Live = true;
            RefreshRate = 4;
        }
        else
        {
            Live = true;
            RefreshRate = Convert.ToInt32(RefreshRateChoice);
        }
        if (data.Chat.Read(MessageType, ChannelChoice) != ContentPresenter.Text && Live)
        {
            RefreshMessages();
        }
        Refresher.Interval = RefreshRate;
        Refresher.Start();
    }




    public void RefreshMessages() {
            if (data.Chat.Read(MessageType, ChannelChoice) != ContentPresenter.Text)
            {
                ContentPresenter.Text = data.Chat.Read(MessageType, ChannelChoice);
            }
    }

and if you need it: 如果需要的话:

        public static string Read(MessageTypes Type, string Channel)
        {
            string Loc;
            if (Type == MessageTypes.Public && (Channel == "1" || Channel == "2"))
            {
                return "Can not view this channel, only post to it.";
            }
            if (Type == MessageTypes.Public)
            {
                Loc = data.AssetsFolder + "\\pub" + Channel + ".zzc";
            }
            else if (Type == MessageTypes.Private)
            {
                Loc = data.AssetsFolder + "\\" + Channel + ".zzpc";
            }
            else if (Type == MessageTypes.Game)
            {
                Loc = data.AssetsFolder;
            }
            else
            {
                Loc = data.AssetsFolder;
            }
            while (true)
            {
                try
                {
                    String MessageList = "";
                    StreamReader MessageReader = new StreamReader(Loc);
                    string EncMessages = MessageReader.ReadToEnd();
                    MessageReader.Dispose();
                    List<string> EncMsgList = EncMessages.Split(';').ToList();
                    for (int i = 1; i < EncMsgList.Count; i++)
                    {
                        MessageList += data.Encodings.Decrypt(EncMsgList[i], Palettes.Message) + "\n";
                    }
                    return MessageList;
                }
                catch
                {
                    // Do nothing
                }
            }
        }

You say that it "freezes." 您说它“冻结”。

In your Read method you have a while(true) loop with an embedded try...catch block, but the catch never returns you from that method. 在您的Read方法中,您有一个while(true)循环和一个嵌入式try ... catch块,但是catch永远不会使您从该方法返回。 If you keep throwing the same exception, you'll continue to loop over and over which could be where you are freezing. 如果您继续抛出相同的异常,那么您将继续循环遍历,这可能是您冻结的地方。

At least to prove that is the case, put a return in you catch or some diagnostic code to indicate if that is the case. 至少要证明是这种情况,请在您的捕获内容中添加退货或一些诊断代码以表明情况是否如此。

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

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