Progress Bar

In a section of my program i am having it pull lines from a text file and place them as a comma separated list in an array.  
this is all done in a for loop.  is there an easy way that i can add a progress bar to my program to let the user see how far along the process is?
Who is Participating?
ddayx10Connect With a Mentor Commented:
Whew, interresting question, only way I can see to do it is with AJAX and it would only be as accurrate as the network traffic allowed.... I think I would consider just faking it. Obviously any of these solutins are going to need AJAX (you are likely already using it or you wouldn't be asking about this).

A) Just use a progress bar made of an animated gif that runs a continual loop until you get your results back.

B) Do some testing and findout the avg time it takes to run with N lines, then use the count of the loop to create an approximated timer. Could be actual countdown, or use a little math to fill a box with pixels relative to the time needed...

C) Just tell'em it's gonna take awhile (depending on their input) and to be patient :)

I'll be interrested to see if anyone comes up with a good answer for this!
Anurag ThakurConnect With a Mentor Technical ManagerCommented:
you cannot find the time required to read the text out of files and then add them to an array easily and the best approach will be to just create a processing image and show so that the user knows that the process is going on and they will have to wait for a while
PablissimoConnect With a Mentor Commented:
Assuming you're using a StreamReader, and you're using ReadLine, you can query the StreamReader's BaseStream property for a Stream object, use that object's Length and Position properties to get how far through the file you are. Set your progress bar maximum to be the length of the file (say), and update the progress bar value to equal your reader.BaseStream.Position calling Application.DoEvents as required to update the UI.

This will only be an approximation, and may not work every time since buffering'll mean that Position jumps a bit (many calls to ReadLine might yield the same Position property value) but it might be sufficient if you have a large file.

StreamReader myReader = new StreamReader( @"C:\TheTextFile.txt" );
long lenBytes = myReader.BaseStream.Length;
while ( !myReader.EndOfStream )
  string theLine = myReader.ReadLine();
  // Do whatever parsing you're doing
  // Replace me with progress bar update code
  Console.WriteLine( "{0:p2} complete", ( (float) myReader.BaseStream.Position ) / lenBytes );

Open in new window

cotj73Author Commented:
I endedup using the progress bar function built into Visual Studio. you can set a max Value and increase it.  allowing an animation for progress
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.