Oztaco
Oztaco

Reputation: 3459

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. The method data.Chat.Read(MessageTypes type, string Channel) infinite loops through a try catch statement till it returns the messages. 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). 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. 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(...)

Refresher.Stop() and .Start() is also not it, worked fine before

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

Upvotes: 2

Views: 1500

Answers (1)

Brad Rem
Brad Rem

Reputation: 6026

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

Upvotes: 1

Related Questions