Solved

Progress Bar

Posted on 2009-07-06
4
562 Views
Last Modified: 2012-05-07
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?
0
Comment
Question by:cotj73
4 Comments
 
LVL 20

Accepted Solution

by:
ddayx10 earned 175 total points
ID: 24791470
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!
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 150 total points
ID: 24791801
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
0
 
LVL 5

Assisted Solution

by:Pablissimo
Pablissimo earned 175 total points
ID: 24792772
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

0
 
LVL 1

Author Comment

by:cotj73
ID: 24851738
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
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now