StringReader not "exiting"/breaking

175 Views Asked by At

I am experiencing a problem with the below code… When I run it the code never exits. I have tried to debug this for about an hour and I am at a complete loss as to what the problem is. OutputDebug(); is essentially like Console.WriteLine();

//Iterate through all lines to find sections
            using (StringReader lineReader = new StringReader(Program))
            {
                int i = 0;
                string line = string.Empty;
                while (line != null)
                {
                    line = lineReader.ReadLine();
                    if (line != string.Empty)
                    {
                        //If the program is exiting (doExit == true), then break the lööp bröther
                    if (doExit)
                            break;

                        //Iterate through all characters in the line
                        foreach (char Character in line)
                    {
                        i++;
                        OutputDebug(i.ToString());
                        if (isOnSameLineAsSectionStart)
                        {
                            sectionName += Character;
                        }
                        else if (Character == ':' && sectionName == string.Empty)
                        {
                            isOnSameLineAsSectionStart = true;
                        }
                        else if (Character == ':' && !isOnSameLineAsSectionStart && sectionName != string.Empty)
                        {
                            OutputDebug("End of section \"" + sectionName + "\" found");
                            OutputDebug(linesInSection.Count() + " lines found total in " + sectionName + "\" during section search");
                            try
                            {
                                sections.Add(sectionName, linesInSection);
                            }
                            catch (Exception)
                            {
                                OutputError("Two/Multiple sections with the same names exist. Ignoring the latest section with the same name");
                            }
                            linesInSection = new List<string>();
                            sectionName = string.Empty;
                            isOnSameLineAsSectionStart = true;
                        }
                    }
                    if (!isOnSameLineAsSectionStart && sectionName != string.Empty)
                    {
                        linesInSection.Add(line);
                    }
                    if (isOnSameLineAsSectionStart && sectionName != string.Empty)
                    {
                        OutputDebug("Start of section \"" + sectionName + "\" found");
                    }
                    if (isOnSameLineAsSectionStart == true)
                    {
                        isOnSameLineAsSectionStart = false;
                    }
                }
                lineReader.Close();
                OutputDebug("In StringReader!" + i);
            }

Thanks in advance!

3

There are 3 best solutions below

0
z3nth10n On

Well, if you want to output all the characters line by line. You could split them into an array of strings:

var lines = Regex.Split(input, "\r\n|\r|\n") extracted from here.

Later, using a foreach instead of a while statment you should solve the problem:

foreach(string line in lines)

Also comparing a string to a null value... Doesn't look so fine. Why don't use (in-built) string.IsNullOrEmpty(line) method to check if the current line is null?

If you want to use your approach you should do something like this:

while (!string.IsNullOrEmpty(line = reader.ReadLine()))
{
    // Your code...
}

Hope this helps!

2
Derviş Kayımbaşıoğlu On

you can use while approach below:

while ((line = reader.ReadLine()) != null)
{
    foreach (char Character in line)
    {
        i++;
        OutputDebug(i.ToString());
    }
}
0
sardok On

Since we're limited to this snippet we can only make assumptions.

lineReader.ReadLine();

if this is a blocking call than the code may never exit as long as no input is given.

if this is a unblocking call, it means that it returns something even though no input is provided. If returned value is empty string in this case then you're in infinite loop.

I believe the key function is ReadLine here.