I am using streamreader to read lines of text from a file. I would like to indicate on a progress bar the status of the reading process. What would be the best way to determine where I am within the file so that I could divide this number by the filename.Length member to display in my statusBar I'm having trouble finding the right method for indicating where the file pointer is...

How to determine file reading progress
Phillip M. Hoff
Mattias,
Thanks! I'll give that a try.
Dennis Rose
You can try checking yourStreamReader.BaseStream.Position. I guess it could be a bit off though due to buffering done by the StreamReader.
amanda.black
Jon is correct, you should run this process in a seperate thread. It's more work but it is more elegant. If you're using the 2.0 Framework try using a BackgroundWorker (MSDN Link) which is designed specifically for this purpose and is designed for reporting back percent complete to progress bars and the such. Your user will get a much more responsive interface as well.
Good luck!
homeslick
Christpher,
That did the trick! Thanks!
cgMarcos
russr
You shouldn't really read the file in the UI thread. Calling Application.DoEvents is usually a sign that you should really be using a different thread to start with (and using Control.Invoke/BeginInvoke to query/update the UI).
See my article on Windows Forms threading for more information.
Jon
Yabby
Jon,
Thanks for the tip, however, I am reading a HEX file into my bootloader and want to show the progress of the firmware programming as it progresses. I figured that showing the file read progress would be a good indication that the firmware update was actually taking place. This gives the nervous user some confidence that he did the right thing!
Martin Sawicki
You don't use Control.Invoke to read the file. You use a different thread to read the file, then Control.Invoke from that other thread to update the UI. (The delegate you pass in to do the update will actually be executed in the UI thread.)
And yes, you definitely still do it in a different thread (IMO).
Jon
henry neo
Thanks for the tip, however, I am reading a HEX file into my bootloader and want to show the progress of the firmware programming as it progresses. I figured that showing the file read progress would be a good indication that the firmware update was actually taking place. This gives the nervous user some confidence that he did the right thing!
And that's why I suggested updating the UI, but doing so using Control.Invoke from another thread. There's no conflict between "don't lock up the UI thread" and "give the user feedback". See the article I referenced for more information.
Jon
stitch23
Bartosz Bien
Is there some trick to updating the progressBar while I loop through a method The progress bar calculation works fine:
progressBar1.Value=(
int)((sr.BaseStream.Position/sr.BaseStream.Length)*100);...but the progress bar does not get updated until I exit the method..
Shri Borde
Jon,
Thanks again for the additional feedback. I have glanced through your article will study it later since it will fix another problem I am having.
One question though. In my C# application I am reading a HEX file line-by-line. Each line is approximately 40 characters long. After reading in the 40 character line I format it and send a command to my target device to program that line into flash memory. Since I am reading such short lines and processing them one-by-one, should I still use the Control.Invoke in another thread to read the file The whole read/write process takes about 5 seconds.
ETJorg
Jon,
OK, thanks. I've got some homework to do.
Paul Smith - Sagestore
Christopher,
Thanks. I'll give that a try also!