Your code sample is incomplete, so it's difficult to say exactly what the problem might be - you don't show where
i
is changed for example.
But ... This comment suggests the problem:
If you are adding each line to a control, then it's likely that the problem is control replaced, and that you are trying to show too much data at once. If you are adding it to a ListBox, or a DataGridView for example then you will run out of Handles long before you run out of memory, and that will trigger the same error message.
It's pointless just "throwing" that much data at your user anyway, so page it, filter it, search it - but just expecting the user to work with thousands of lines is just going to annoy them!
Remove the code thgthgat actually displays the lines, and I'm pretty sure the error message will go as well...