0

I have a problem that I have been trying to figure out for hours now, and I am sure it's something simple.

Here is the code (extra junk removed as it's not a problem).

foreach (String itemChecked in fightsList.CheckedItems)
{
    try
    {
        Thread.Sleep(50);
        using (StreamReader reader = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.ASCII))
            {
                while ((line = reader.ReadLine()) != null)
                    {
                        // do all stuff to lines here...
                    }
            }
    }
    catch (Exception error)
        {
            errorText.Text = error.ToString();
        }
}
// foreach done here.

I am reading a text file, obvious from StreamReader, but when the foreach loop rolls to the second iteration, it fails before running the StreamReader and stops the foreach loop.

The first loop runs great.

I am thinking it's a problem because the 'StreamReader reader = new' already exists and it cannot create a new one?

The foreach is a string checkedBox. StreamReader is reading the same file for all instances in the loop, I just handle where to start and stop per each. My error catching does not get anything, and the application does not crash.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1270029
  • 85
  • 2
  • 7
  • 3
    What is the error message that you receive? – Jetti Mar 14 '12 at 20:20
  • I do not get any error messages. That's sort of the problem. It runs normal, hits second iteration, and then drops to below the foreach and runs code as normal as if the foreach was done. But during the second run, inside the foreach, code above the StreamReader does get executed. Maybe I need to add some more code to catch other errors that are happening? – user1270029 Mar 14 '12 at 20:23
  • 1
    Could you tell us what's fightsList? A listbox or? – Steve Mar 14 '12 at 20:23
  • Is there a reason why your putting the Thread to sleep? Try eliminating that and see if that helps – Jetti Mar 14 '12 at 20:25
  • Yes, a checkedListBox. System.Windows.Forms.CheckedListBox – user1270029 Mar 14 '12 at 20:25
  • I have removed it, was for slowing down to see if possibly the garbage collection had to remove the reader object and such. I'll make another run without it. – user1270029 Mar 14 '12 at 20:27
  • 1
    Try checking more than one item. – Hans Passant Mar 14 '12 at 20:31
  • 1
    Standard -1 for "it fails" without any error information. – H H Mar 14 '12 at 20:44

2 Answers2

2

I think you have a logic problem, in that you keep trying to read from the same file with each iteration through the loop (unless your removed something relevant from your code). If you want to read from the same file after each loop, than you should only need to create the stream reader once, outside the outer for loop. In addition, since you are not closing the file, you could be running into issues with being at the end of the file after the first iteration. That would explain why it only works the first time

javram
  • 2,635
  • 1
  • 13
  • 18
  • 1
    "using" should automatically take care of closing the file, so I don't think that's the problem. – Matt Burland Mar 14 '12 at 20:29
  • So without messing with all of the code, I should try to reset the position of the StreamReader to the beginning of the file as a start. – user1270029 Mar 14 '12 at 20:30
  • 1
    Sorry, this is not fully true. The stream is used inside a `using` statement and should be closed after the using statement. – GETah Mar 14 '12 at 20:31
  • I think the logic error still applies here, if the filename never changes between the iterations of the loop, why read it again each time. – javram Mar 14 '12 at 20:33
  • I'm actually pulling specific groups of lines out of that file, depending on what the user wants. Since you have to load the file to see where a line is, I make a fast run first go (not in that code example) to get values into the listbox, and from those values I know where to start and stop inside the file when it runs. First runs great from any point, just the second onward do not. – user1270029 Mar 14 '12 at 20:36
  • Are you sure you're not pulling ALL the lines out? That would explain the loop ending without an error. It would readLine null. – Ryan Bennett Mar 14 '12 at 20:41
  • Yep, I'm inserting all pulled lines into an SQL server and its pulling exactly the lines I'm telling it to. I moved the FileStream out of the foreach same result/ – user1270029 Mar 14 '12 at 20:45
  • @GETah http://stackoverflow.com/questions/1065168/does-disposing-streamreader-close-the-stream – L.B Mar 14 '12 at 20:46
  • How big is the file? Would an easier solution be to read the file into memory one time (using an array with one line of text in each index). That way your significantly reduce the amount of IO against the disk, since you are only reading the file one time. – javram Mar 14 '12 at 21:17
  • @javram we all know that there may be many improvements in the code. But the problem is why it fails in second iteration. – L.B Mar 14 '12 at 21:28
0

The problem is not in the code you have posted. I would suggest allocating the file stream as a separate step and putting a break point on the line where you are passing it into the StreamReader. On the second pass make sure everything (filename, length, etc) looks OK on the underlying stream your problem is inside of the loop otherwise it is elsewhere in your code.

your also better off reorganizing your code like

using(StreamReader reader = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.ASCII)){
    while ((line = reader.ReadLine()) != null)
    {
        foreach (String itemChecked in fightsList.CheckedItems)
       {
       }
    }
}

since this will make the expensive operation (file IO) happen as few times as possible, but that isn't why you are having this specific problem.

Yaur
  • 7,333
  • 1
  • 25
  • 36