1

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!

PixxlMan
  • 69
  • 1
  • 3
  • 11
  • 1
    Yeah, look what the content of the `line` variable is (the variable you use as exit condition for the `while` loop). Do you notice something? –  Dec 14 '18 at 19:25
  • `i` is never set to `null` so the `while` loop's condition will never be `true` (and won't terminate). – Lews Therin Dec 14 '18 at 19:27
  • Why do you don't `string.IsNullOrEmpty()` method? – z3nth10n Dec 14 '18 at 19:27
  • @elgonzo oops! I accidentally left out this piece of code: line = lineReader.ReadLine(); – PixxlMan Dec 14 '18 at 19:28
  • @Jimi the "Program" parameter is not an empty string – PixxlMan Dec 14 '18 at 19:31
  • Well, unless the string in `Program` is infinite (which is not possible), your program will eventually exit. With a NullReferenceException... –  Dec 14 '18 at 19:31
  • I have edited the comment @Richardissimo – PixxlMan Dec 14 '18 at 19:31
  • @elgonzo Why will it exit with a NullReferenceException? – PixxlMan Dec 14 '18 at 19:32
  • you get a line then immediately use it - even if its null. – pm100 Dec 14 '18 at 19:33
  • What do you expect `line` to become if the StreamReader has reached the end and you try `line = lineReader.ReadLine();`? Then, what are you trying to do with `line` right after this? –  Dec 14 '18 at 19:33
  • Sorry, the original code was a lot longer I just deleted the unneeded parts. Character was used in the original code. – PixxlMan Dec 14 '18 at 19:33
  • 1
    what did you see when you stepped through with the debugger? – pm100 Dec 14 '18 at 19:33
  • 1
    ok, so this code is your summary of what you think is having the problem, but that code wont even run. You have to show us code that actually has the issue you describe. We are not magicians – pm100 Dec 14 '18 at 19:35
  • @elgonzo null I would guess? Its what I´m checking for with the While-loop, maybe I have to check inside the loop too? – PixxlMan Dec 14 '18 at 19:35
  • We can only see and examine the code in your question here. If your original code does not exit/break, then the code in your question here is clearly not representative of your problem (i.e., it does not demonstrate it) –  Dec 14 '18 at 19:35
  • Of course you are checking for `null` in the while loop. That's not what i asked for . I asked specifically: Then, what are you trying to do with line right after executing `line = lineReader.ReadLine()`? –  Dec 14 '18 at 19:38
  • @elgonzo Allright, here is the whole code. Excuse how messy it is. Its not finished yet – PixxlMan Dec 14 '18 at 19:38
  • @elgonzo Thanks! However does this work when the code is in an external library? The code shown above is in a dll which is used by a winforms application. – PixxlMan Dec 14 '18 at 19:48
  • Add the project of the external library as part of your solution in Visual Studio. Make your main project dependent on that library project. Then you can debug into this library directly without any hassle. –  Dec 14 '18 at 19:49

3 Answers3

3

you can use while approach below:

while ((line = reader.ReadLine()) != null)
{
    foreach (char Character in line)
    {
        i++;
        OutputDebug(i.ToString());
    }
}
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
1

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!

z3nth10n
  • 2,341
  • 2
  • 25
  • 49
1

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.

sardok
  • 1,086
  • 1
  • 10
  • 19