Lore Posted June 1, 2003 Posted June 1, 2003 I'm developing an application, and one of it's functions is to read files. i want to add a progress bar which indicates how many precents of the file have been read. the problem is that with big files the program hangs until the operation is completed, and no info is shown in the progress bar. i'm working with j#.net but it can happen in any language. here's a bit of the code: public void readFile(String fileName) { FileStream fs =new FileStream(fileName,FileMode.Open); long fileSize = fs.get_Length(); fs.Close(); StreamReader reader = new StreamReader(fileName); //the form where the progress bar is: progressWindow.Show(); int bytesRead = 0; //number of bytes that have been read. char buf = '\0'; //buffer while (reader.Peek() != -1) //while not at the end of the file... { buf = (char)(reader.Read()); /*...*/ bytesRead++; //update the progress bar: progressWindow.progressBar.set_Value((int)((bytesRead/fileSize)*100)); } reader.Close(); writer.Close(); progressWindow.Hide(); } the problem is when i'm trying to update the progressBar with set_Value(), it just doesn't work. thanks for your help. Quote
*Experts* mutant Posted June 1, 2003 *Experts* Posted June 1, 2003 Have you tried this? Application.DoEvents(); Put this in your loop. It will cause windows to process other messages. Quote
Leaders dynamic_sysop Posted June 1, 2003 Leaders Posted June 1, 2003 // // progressBar1 // this.progressBar1.Location = new System.Drawing.Point(40, 264); this.progressBar1.Maximum = 10000; this.progressBar1.Minimum = 1; this.progressBar1.Name = "progressBar1"; this.progressBar1.Size = new System.Drawing.Size(280, 23); this.progressBar1.Step = 1; this.progressBar1.TabIndex = 4; this.progressBar1.Value = 1; // // private void button3_Click(object sender, System.EventArgs e) { for(int i=progressBar1.Minimum; i<=progressBar1.Maximum; i++) { progressBar1.PerformStep(); } } that causes the progressbar to go from 0 to maximum when hitting a button , maybe it'll give you ideas on your problem Quote
Lore Posted June 1, 2003 Author Posted June 1, 2003 mutant, the readFile procedure is coming from a dll, outside the application, so i can't use your suggestion. dynamic_sysop, i need the progress bar to update simultaneously while reading the file, according to the number of bytes already read. besides the problem is not updating the progress bar, but updating it while reading the file. right now the program just hangs until it finishes reading the file... Quote
Lore Posted June 2, 2003 Author Posted June 2, 2003 i've added the executables so you can see the problem for yourself. please take a look and tell me if you have an idea... if you try to encode a big file, you'll see the problem. (just click on the 'File' button at the bottom right, and choose a file). btw, the decoding proccess is a little buggy, so don't try it...sende2.zip Quote
Leaders dynamic_sysop Posted June 2, 2003 Leaders Posted June 2, 2003 private void button1_Click(object sender, System.EventArgs e) { readFile("fileread.txt"); } ///// //// public void readFile(String fileName) { FileStream fs = new System.IO.FileStream(fileName,FileMode.Open,FileAccess.Read); long fileSize = fs.Length; int lngth = Convert.ToInt32(fileSize); Console.WriteLine(lngth); this.progressBar1.Minimum = 1; this.progressBar1.Maximum = lngth; this.progressBar1.Step = 1; this.progressBar1.Value = 1; fs.Close(); StreamReader reader = new StreamReader(fileName); int bytesRead = 0; //number of bytes that have been read. char buf = '\0'; //buffer while(reader.Peek()!= -1) { buf = (char)(reader.Read()); /*...*/ bytesRead++; //update the progress bar: this.progressBar1.Value = bytesRead; //progressWindow.progressBar.set_Value((int)((bytesRead/fileSize)*100)); } reader.Close(); } i just spent a few minutes sussing the filestream out, i've done quite a bit on progressbars in vb6 and put my logic from vb6 to it and it works fine this way. hope this helps a bit. Quote
Lore Posted June 2, 2003 Author Posted June 2, 2003 that helped, thanks. there are few other poblems but i think i'll take it from here. :D Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.