Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • Leaders
Posted

// 
// 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

Posted

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...

Posted

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

  • Leaders
Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...