Progress bar during file processing

G

Guest

Hello,

I am reading a number of files in a directory and doing some work after
reading each line of the file. What I want to have is a progress bar that
gets updated as each file is being read.

How can I find the values to use when updating the progress bar? I am using
a StreamReader to read the files and whenever I start working on a new file I
set the progress bar's maximum value to sr.BaseStream.Length. Then, I was
going to increment the progress bar as I read each line (by the size of the
line). I am reading each line like so:

using (StreamReader sr = new StreamReader(file))
{
while((line = sr.ReadLine()) != null)
{
...
}
}

I don't know how to find the number of bytes of that line though. I took
the number of chars in the line and divided by two (a char is 2 bytes right?)
but it doesn't add up to the number of bytes returned by the stream length
property. Does BaseStream.Length contain some extra information?
Is this even the right way to do it?

Any tips are appreciated.
Thanks,
-Flack
 
J

Jon Skeet [C# MVP]

Flack said:
I am reading a number of files in a directory and doing some work after
reading each line of the file. What I want to have is a progress bar that
gets updated as each file is being read.

How can I find the values to use when updating the progress bar? I am using
a StreamReader to read the files and whenever I start working on a new file I
set the progress bar's maximum value to sr.BaseStream.Length. Then, I was
going to increment the progress bar as I read each line (by the size of the
line). I am reading each line like so:

using (StreamReader sr = new StreamReader(file))
{
while((line = sr.ReadLine()) != null)
{
...
}
}

I don't know how to find the number of bytes of that line though. I took
the number of chars in the line and divided by two (a char is 2 bytes right?)

A char is two bytes in memory, but may not be two bytes in the file -
that entirely depends on the encoding.
but it doesn't add up to the number of bytes returned by the stream length
property. Does BaseStream.Length contain some extra information?
Is this even the right way to do it?

You could use BaseStream.Position vs BaseStream.Length, but don't
forget that StreamReader buffers its input, so the position will
usually be somewhat ahead of what you've really read.
 
R

Roger Rabbit

Are you sure this is even neccessary? Why not just increment the progress
bar 1 click per line? Are you actually adding anything to the user
experience but incrementing byte by byte?

Its sounds like you might end up using just as much system resource to
increment your progress bar and you might doing the things which progress
it. You might well end up needing a progress bar for your progress bar.

;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top