5 Replies - 4403 Views - Last Post: 06 November 2011 - 06:16 PM Rate Topic: ***** 1 Votes

#1 q12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 12-April 09

Incremental load for progressBar in webbrowser

Posted 05 November 2011 - 05:44 PM

-=Incremental load for progressBar in webbrowser=-
When a page is loading, the progressBar is "Loading" multiple times, with this code:
        //progressbar for page loading
        int max = 0;
        int now = 0;
private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
                max = Convert.ToInt32(e.MaximumProgress);
                now = Convert.ToInt32(e.CurrentProgress);

                progressBarPageLoading.Maximum = max;
                if (now > 0 && now < max)//now = -1 sometimes and greater than max other times; strange.
                {
                    progressBarPageLoading.Value = now;
                }   
        }

I want that progressBar to display an incremental load (not starting from 0 a couple of times per page).
And why it does that, by the way? (i assume because of the javascript behind it/ or multiple forms,images,headings)?
Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Incremental load for progressBar in webbrowser

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4310
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: Incremental load for progressBar in webbrowser

Posted 05 November 2011 - 06:30 PM

Now the currentprogress and maxprogress return "long" data types for a reason. The values here can be huge. Much bigger than integers. So you shouldn't convert them to ints. But I know why you are, because the progress bar deals in integers.

So instead of trying to stuff large values into max/min, why not figure out the percentage and use that on a scale of 0 to 100?

private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e) {
    // Get the max and min progress in bytes (longs)
    long max = e.MaximumProgress;
    long now = e.CurrentProgress;

    // Percentage starts at zero
    Int32 percentage = 0;

    // Make sure we never get a divide by zero
    if (max > 0) {
        // Create a percentage and put that into an integer
        percentage = Convert.ToInt32((now / max) * 100);
    }

    // Now stick the percentage itself into the progress bar 
    progressBarPageLoading.Value = percentage;
            
}



With this code above, we need to make sure that we set the progress bar max and min to 100 and 0 respectively, but OUTSIDE of this function. This function is going to potentially fire hundreds of times. No use setting the min and max that many times.

Hopefully this will solve the problem. :)
Was This Post Helpful? 2
  • +
  • -

#3 q12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 12-April 09

Re: Incremental load for progressBar in webbrowser

Posted 05 November 2011 - 07:14 PM

Yes - it works.
I like the percentage part. :) I don't have a mathematician mind,and it show. :)
I adjusted a bit, your code:

        //still [NoN]Incremental load for progressBar in webbrowser
        private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            // Get the max and min progress in bytes (longs)
            long max = e.MaximumProgress;//  |10000 |1000000 |1000000
            long now = e.CurrentProgress;//  |100   |1050600 |100

            // Percentage starts at zero
            Int32 percentage = 0;

            // Make sure we never get a divide by zero
            if (max > 0)
            {
                long divide = now / max;
                // Create a percentage and put that into an integer
                percentage = Convert.ToInt32((divide) * 100);


                if (percentage > 100)//for the 3'd_case > [1000000|100] 
                {
                    percentage = 100;
                }
            }

            // Now stick the percentage itself into the progress bar 
            progressBarPageLoading.Value = percentage;

        }



Though is doing much better now than before... but STILL... not incremental.
I want to see that progress how is "stacking up".
Watch at FireFox loading page (now inserted in the tab, a little line as progressBar).
try loading something like youtube page or cgsociety page. It behave like I say.
Of course i can make it(adding a counter or a pivot), but i love the math you come so far, and im curious how you do it.
Thanks.

This post has been edited by q12: 05 November 2011 - 07:17 PM

Was This Post Helpful? 0
  • +
  • -

#4 q12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 12-April 09

Re: Incremental load for progressBar in webbrowser

Posted 05 November 2011 - 09:11 PM

+5stars for you....but i would like to continue it though.... if possible..
I have more important stuff to uncover, but i don't like unfinished business.
Thanks for your straight answers, until now.
Was This Post Helpful? 0
  • +
  • -

#5 q12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 12-April 09

Re: Incremental load for progressBar in webbrowser

Posted 06 November 2011 - 03:02 PM

so...any thoughts?
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4310
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: Incremental load for progressBar in webbrowser

Posted 06 November 2011 - 06:16 PM

I am not sure what you are talking about and what you mean by "stacking up". As data comes in and you figure out the percentage, it will increment the bar. Now if the total number of bytes is relatively small (like fetching a website which can be only a few hundred kilobytes), the bar will appear to go quick. However, if you are downloading a large file, the progress bar will increment one percentage at a time because it is going to take longer.

Have you tried the above process with a large file and not a webpage?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1