WPF progress bar not showing proper progress

I have an application in which I upload a file to blocks. My external interface is WPF, and I have a progress bar showing the progress of downloading the file (the download is done in a separate thread, and the progress bar is in a separate form called by the child thread when the download starts).

I found the total number of blocks in the file to set the maximum progress bar property.

Now for each loaded block, I increase the value of the progress bar by 1.

But, to my surprise, the progress bar starts to increase, but never ends (it stops showing progress after several blocks).

Here is the code for the stream responsible for downloading the files:

  System.Threading.Thread thread = new Thread (
    new ThreadStart (
          delegate ()
          {
               // show progress bar - Progress is the name of window containing progress bar
               Progress win = new Progress ();
               win.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
               win.Show ();

               // find number of blocks
              long BlockSize = 4096;
              FileInfo fileInf = new FileInfo (filename);
              long FileSize = fileInf.Length;
              long NumBlocks = FileSize / BlockSize;

              // set the min and max for progress bar
              win.Dispatcher.Invoke (
                   new Action (
                      delegate ()
                      {
                             win.progressBar1.Minimum = 0;
                             win.progressBar1.Maximum = NumBlocks;                                                        
                      }
              ), System.Windows.Threading.DispatcherPriority.Render);

              // upload file
              while (true)
              {

                       // code to upload the file

                       win.Dispatcher.Invoke (
                           new Action (
                               delegate ()
                               {
                                  win.progressBar1.Value + = 1;
                               }
                       ), System.Windows.Threading.DispatcherPriority.Render);

              }
        }

Can someone help me analyze why this is happening.

Thanks.

+4
source share
1 answer

Here's the problem:

Loading

executed by a separate thread and the progress bar is in a separate form called by the child thread when the download starts

If this means that your child thread created the form, then this is the problem. Your child thread may update progress bar values, but this will only display invalidate , and not necessarily update the display. When the control display is not valid , he simply writes that he should redraw it the next time he gets a chance. A refresh is when the control actually gets to display on the screen.

A better approach is to create a progress bar form in the main thread.

Then your workflow can update the status, and your main thread updates the display.

Remember: if you are updating a control that was created in another thread, you must do this through the control manager.

var dispatcher = progressBar.Dispatcher; dispatcher.BeginInvoke(new Action( () => { progressBar.Value = currentProgress })); 


Change after viewing the code

All you have to do is move the creation of the progress variable so that it is created by the main thread before the workflow is created.


 Progress win = new Progress(); win.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen; win.Show(); System.Threading.Thread thread = new Thread( new ThreadStart( delegate() { // ... 
+6
source

All Articles