In this case the progressBar is moving faster then the real Dowork progress then throw exception in the progressChanged event that the progressBar value is over 100% then whats wrong here how do i calculate and report it the right way ?
private List<string> webCrawler(string url, int levels , DoWorkEventArgs eve)
{
bool already = false;
HtmlAgilityPack.HtmlDocument doc;
HtmlWeb hw = new HtmlWeb();
List<string> webSites;// = new List<string>();
List<string> csFiles = new List<string>();
csFiles.Add("temp string to know that something is happening in level = " + levels.ToString());
csFiles.Add("current site name in this level is : " + url);
try
{
doc = hw.Load(url);
webSites = getLinks(doc);
if (levels == 0)
{
return csFiles;
}
else
{
int actual_sites = 0;
for (int i = 0; i < webSites.Count() && i < 20; i++) {
if ((worker.CancellationPending == true))
{
eve.Cancel = true;
break;
}
else
{
string t = webSites[i];
if ((t.StartsWith("http://") == true) || (t.StartsWith("https://") == true)) // replace this with future FilterJunkLinks function
{
for (int e = 0; e < csFiles.Count; e++)
{
if (csFiles[e].Contains(t))
{
already = true;
}
}
if (!already)
{
actual_sites++;
csFiles.AddRange(webCrawler(t, levels - 1,eve));
this.Invoke(new MethodInvoker(delegate { Texts(richTextBox1, "Level Number " + levels + " " + t + Environment.NewLine, Color.Red); }));
worker.ReportProgress(i * 10);
}
}
}
}
return csFiles;
}
}
catch
{
return csFiles;
}
}
public void Texts(RichTextBox box, string text, Color color)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
worker = sender as BackgroundWorker;
webCrawler(guys, 2,e);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
I passed the variable e of the DoWorkEventsArgs in the DoWork event so i can use the worker variable in the webCrawler function if i need to cancel the operation. In the webCrawler im calling it DoWorkEventArgs eve.
The problem is that the report to the progressBar is not the right way.
Tried the reportprogress as:
for (int i = 0; i < webSites.Count() && i < 20; i++) {
double incPercent = (1 / webSites.Count());
if ((worker.CancellationPending == true))
{
eve.Cancel = true;
break;
}
else
{
string t = webSites[i];
if ((t.StartsWith("http://") == true) || (t.StartsWith("https://") == true)) // replace this with future FilterJunkLinks function
{
for (int e = 0; e < csFiles.Count; e++)
{
if (csFiles[e].Contains(t))
{
already = true;
}
}
if (!already)
{
actual_sites++;
csFiles.AddRange(webCrawler(t, levels - 1,eve));
this.Invoke(new MethodInvoker(delegate { Texts(richTextBox1, "Level Number " + levels + " " + t + Environment.NewLine, Color.Red); }));
worker.ReportProgress(i * (int)incPercent);
Anytime you have i > 10 (That is, more than 10 pages), you are going over 100% in your progress bar.
If you really wanted to do it by percentage, I would do something like this:
As a side note, your variable names and spacing seem.. a little bit off
Edit: Sorry, when performing a division of two numbers and you want the result to be in double, you have to make sure that either divisor or dividend is of type double. Otherwise, you are effectively performing int / int, which gives you an int back. As you probably know, int truncates your decimal points. This is the reason why you had zero in the calculation.
Also, reportProgress method takes values from 0 to 100 so you have to multiply the end result by 100.
Fix your code like this: