By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,978 Members | 1,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,978 IT Pros & Developers. It's quick & easy.

Loop to read file and exit on button click

P: n/a
Hello. I am trying to write a program that continues to read the last
line of a txt file until a stop button is clicked.

while (ContinueLoop == true )
{
while ((LastLine = file.ReadLine()) != null)
{
LastLine = file.ReadLine();
}//while readline

CurrentTime = DateTime.Now;
lblTime.Text = CurrentTime.ToString();
lblCounter.Text = Counter.ToString();
lblTime.Refresh();
lblString.Refresh();
lblCounter.Refresh();
Application.DoEvents();
Counter++;
lblString.Text = LastLine;

} // While do until button pressed

I have two While loop, once running until a stop button is pressed, and
the other running until the end of the file is found and the last value
assigned to a variable. Problem is that the program runs fine the
first time is passes through the loop but then keeps skipping the inner
loop...I placed a watch on "file.readline()" and, once it has completed
the first loop, it says (under the value col of the Watch window)
"function 'file.ReadLine()' evaluated and returned null.

How can I "refresh" 'file.ReadLine()' value so that it doesnt keep the
"null status"

Thanks!

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
have solved my problem - sorry...for anyone elses interest the soln was
:

while (ContinueLoop == true )
{
while ((line) != null)
{
LastLine = line;
line = file.ReadLine();

}//while readline

CurrentTime = DateTime.Now;
lblTime.Text = CurrentTime.ToString();
lblCounter.Text = Counter.ToString();
lblTime.Refresh();
lblString.Refresh();
lblCounter.Refresh();
Application.DoEvents();
Counter++;
lblString.Text = LastLine;
file.Close();
} // While do until button pressed
}

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.