How to determine file reading progress

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...

Answer this question

How to determine file reading progress

  • malik anees

    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

     



  • Ti133700N

    Christpher,

     

    That did the trick! Thanks!


  • BarryMarc

    Try adding "System.Windows.Forms.Application.DoEvents();" after you update the value of the progress bar.
  • gambas

    Christopher,

    Thanks. I'll give that a try also!

     


  • scott xxxxxxxx

    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..

     


  • YesK

    Mattias,

    Thanks! I'll give that a try.


  • mthierauf

    Jon,

    OK, thanks. I've got some homework to do.


  • ISU

    You're welcome
  • MisterChief

    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!


  • Ajay1234

    You can try checking yourStreamReader.BaseStream.Position. I guess it could be a bit off though due to buffering done by the StreamReader.

     

     



  • Byaloz

    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



  • Bill.Net

    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



  • jayman16

    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.


  • TechAddict

    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!


  • How to determine file reading progress