Solved

Progress Bar

Posted on 2009-07-06
4
565 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

23 Experts available now in Live!

Get 1:1 Help Now