logical problem

Lore

Member
Joined
Jun 1, 2003
Messages
5
Im developing an application, and one of its 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.

im working with j#.net but it can happen in any language.

heres a bit of the code:
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 im trying to update the progressBar with set_Value(), it just doesnt work.

thanks for your help.
 
Have you tried this?
Code:
Application.DoEvents();
Put this in your loop. It will cause windows to process other messages.
 
Code:
// 
// 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 itll give you ideas on your problem
 
mutant, the readFile procedure is coming from a dll, outside the application, so i cant 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...
 
ive 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, youll 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 dont try it...
 

Attachments

Code:
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, ive 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.
 
Back
Top