Progress bar for my C# browser

I have source code, but it's not working

Page 1 of 1

6 Replies - 16468 Views - Last Post: 22 April 2008 - 07:15 PM Rate Topic: -----

#1 aleisterbukowski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-April 08

Progress bar for my C# browser

Post icon  Posted 22 April 2008 - 02:50 AM

private void WebBrowser1_ProgressChanged(object sender, System.Windows.Forms.WebBrowserProgressChangedEventArgs e)
		{
			int d;
			int t;
			ProgressBar1.Visible = true;
			d = (int)e.CurrentProgress;
			t = (int)e.MaximumProgress;
			if ((d <= 0))
			{
				ProgressBar1.Value = 0;
				ProgressBar1.Visible = false;
			}
			else
			{
				ProgressBar1.Value = Math.Min(ProgressBar1.Maximum, Convert.ToInt32(Math.Floor((ProgressBar1.Maximum
										* (d / t)))));
			}
		}




Error 1 The call is ambiguous between the following methods or properties: 'System.Math.Floor(decimal)' and 'System.Math.Floor(double)' C:\Documents and Settings\carl\My Documents\Visual Studio 2008\Projects\SimpleBrowser\SimpleBrowser\Form1.cs 96 85 SimpleBrowser
[/source]

Is This A Good Question/Topic? 0
  • +

Replies To: Progress bar for my C# browser

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4892
  • View blog
  • Posts: 11,288
  • Joined: 16-October 07

Re: Progress bar for my C# browser

Posted 22 April 2008 - 05:54 AM

You're getting this because inside your "Math.Floor" you're doing this "long * (int / int)". The result is of type long. It doesn't know how to floor a long. Also, because d and t are int, you'll never get the float you're looking for.

Perhaps something like this:
private void WebBrowser1_ProgressChanged(object sender, System.Windows.Forms.WebBrowserProgressChangedEventArgs e) {
	// if it's done or not going, we don't want it visible
	ProgressBar1.Visible = (!(e.CurrentProgress<=0 || e.CurrentProgress==e.MaximumProgress));
	if (ProgressBar1.Visible) {
		// this gets the percentage and multiplies it by integer 100
		// giving an int result between 0 and 100
		ProgressBar1.Value = ((float)e.CurrentProgress/(float)e.MaximumProgress) * 100;
	} else {
		// this is probably complete redundant, since we don't see it
		ProgressBar1.Value = 0;
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 aleisterbukowski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-April 08

Re: Progress bar for my C# browser

Posted 22 April 2008 - 04:46 PM

Error 1 Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\carl\My Documents\Visual Studio 2008\Projects\SimpleBrowser\SimpleBrowser\Form1.cs 90 38 SimpleBrowser

so I just wrapped that with (int)

and it compiled just fine, but the progressbar didn't work at all. I tried myspace.com figuring this time of an hour with the traffic I'd have to get some type of loading problem.

This post has been edited by aleisterbukowski: 22 April 2008 - 04:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Progress bar for my C# browser

Posted 22 April 2008 - 05:26 PM

Try the following to get the progress bar to work in the manner you are expecting.

private void WebBrowser1_ProgressChanged(object sender, System.Windows.Forms.WebBrowserProgressChangedEventArgs e)
{
		ProgressBar1.Maximum = Convert.ToInt32(e.MaximumProgress);
		ProgressBar1.Value = Convert.ToInt32(e.CurrentProgress);
}

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4892
  • View blog
  • Posts: 11,288
  • Joined: 16-October 07

Re: Progress bar for my C# browser

Posted 22 April 2008 - 06:24 PM

View Postaleisterbukowski, on 22 Apr, 2008 - 07:46 PM, said:

so I just wrapped that with (int)

and it compiled just fine, but the progressbar didn't work at all.


Odd. The "* 100" should have forced an implicit int down step of the result. I note you're using VS2008, that may behave differently than I expected. If you're just trying to pull a web page, maybe I'll throw something together and give it a spin...

View Postjayman9, on 22 Apr, 2008 - 08:26 PM, said:

ProgressBar1.Maximum = Convert.ToInt32(e.MaximumProgress);
ProgressBar1.Value = Convert.ToInt32(e.CurrentProgress);



I considered this approach, but didn't like the idea of reinitializing the ProgressBar1.Maximum for every tick. I'd probably initialize it once using a flag, but didn't want to add extra complexity. Still, if it works over the one I offered that didn't, it's the better choice. :P
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4892
  • View blog
  • Posts: 11,288
  • Joined: 16-October 07

Re: Progress bar for my C# browser

Posted 22 April 2008 - 07:05 PM

Alright, just gave this a spin.

private void Form1_Load(object sender, EventArgs e) {
	this.webBrowser1.Navigate("http://www.dreamincode.net/");
}

private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e) {
	// if it's done or not going, we don't want it visible
	Debug.WriteLine("webBrowser1_ProgressChanged: " + e.CurrentProgress + ", " + e.MaximumProgress);
	this.progressBar1.Visible = (!(e.CurrentProgress <= 0 || e.CurrentProgress == e.MaximumProgress));
	if (this.progressBar1.Visible) {
		// this gets the percentage and multiplies it by integer 100
		// giving an int result between 0 and 100
		this.progressBar1.Value = (int)(((float)e.CurrentProgress / (float)e.MaximumProgress) * 100);
	} else {
		// this is probably complete redundant, since we don't see it
		this.progressBar1.Value = 0;
	}

}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
	Debug.WriteLine("webBrowser1_DocumentCompleted: " + e.Url);
}



Here are the trace results:
webBrowser1_ProgressChanged: 100, 10000
webBrowser1_ProgressChanged: 100, 10000
webBrowser1_ProgressChanged: 150, 10000
webBrowser1_ProgressChanged: 200, 10000
webBrowser1_ProgressChanged: 88800, 1000000
webBrowser1_ProgressChanged: 88800, 1000000
webBrowser1_ProgressChanged: 265300, 1000000
webBrowser1_ProgressChanged: 265300, 10000
webBrowser1_ProgressChanged: 265300, 10000
webBrowser1_ProgressChanged: 26817000, 1000000
webBrowser1_ProgressChanged: 26817000, 10000
webBrowser1_ProgressChanged: 27247800, 10000
webBrowser1_ProgressChanged: 27817000, 10000
webBrowser1_ProgressChanged: 27817000, 10000
webBrowser1_ProgressChanged: 33406530, 1000000
webBrowser1_ProgressChanged: 32944730, 1000000
webBrowser1_ProgressChanged: 1000000, 1000000
webBrowser1_DocumentCompleted: http://www.dreamincode.net/ads/adframe.php?n=a965628166bc&what=zone:23&resize=1
webBrowser1_DocumentCompleted: http://www.dreamincode.net/ads/adframe.php?n=a489449152bc&what=zone:6&resize=1
webBrowser1_ProgressChanged: -1, 1000000
webBrowser1_DocumentCompleted: http://a.lakequincy.com/f.ashx?channel=1&format=7&pageid=D0AFA85C-7C6F-304A-F352-724121D821E1&publisher=112&ypos=0&zone=1&country=US&placement=2007&creative=1234&
webBrowser1_DocumentCompleted: http://ads.aspalliance.com/displayad.aspx?t=12&m=115&guid=[guid]
webBrowser1_ProgressChanged: 0, 0
webBrowser1_ProgressChanged: 0, 0
webBrowser1_DocumentCompleted: http://www.dreamincode.net/



Looks like crap, huh? :P

A web page is not just one http call, but an initial fetch and then a get for every the other external element referenced by the document that the browser parses. At to that the non linear nature of events...

In the end, progress bars are all faking. The almost always are. They make a guess and give the user a warm fuzzy. There's probably a clever way to do this with a percentage increment hit each time the ProgressChanged event fires.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Progress bar for my C# browser

Posted 22 April 2008 - 07:15 PM

You have to get MaximumProgress each time the progress changes. For the very reasons you just stated.

As for browsers faking, that is more than likely true. They more than likely take some kind of average based on latency. Just my opinion.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1