Note that there are some explanatory texts on larger screens.

plurals
  1. POReportProgress doesn't call progressChanged with tasks in c#
    text
    copied!<pre><code> private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { int currentProgress=-1; while (currentProgress&lt;length) { currentProgress=Worker.progress; backgroundWorker1.ReportProgress(currentProgress); Thread.Sleep(500); length = Worker.UrlList.Count; } } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { int ix = e.ProgressPercentage; progressBar1.Value = ix; lblText.Text =ix+" %"; } </code></pre> <p>I wrote a program to download page sources by reading a file have about 1000 URLs. so I used Tasks to download pages async. here <code>Worker.progress</code> is the currently executed URL amount. though the debuger hits the <code>backgroundWorker1.ReportProgress(currentProgress);</code> it never enter to the <code>backgroundWorker1_ProgressChanged</code>. </p> <pre><code> private void StartButton_Click(object sender, EventArgs e) { t.makeUrlList(inputFile); backgroundWorker1 = new BackgroundWorker(); backgroundWorker1.WorkerReportsProgress = true; backgroundWorker1.DoWork += backgroundWorker1_DoWork; backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged; backgroundWorker1.RunWorkerAsync(); t.RunTasks(); Application.Exit(); } </code></pre> <p>background worker initializes when start button clicks...</p> <p>here is where my tasks are created....</p> <pre><code>public void RunTasks() { if (numOfTasks &gt; UrlList.Count) numOfTasks=UrlList.Count-1; Task[] t = new Task[numOfTasks]; int j = 0; while ( j &lt; UrlList.Count-1) { for (int i = 0; (i &lt; t.Count())&amp;&amp;(j&lt;UrlList.Count-1); i++) { try { if (t[i].IsCompleted || t[i].IsCanceled || t[i].IsFaulted) { t[i] = Task.Run(() =&gt; FindWIN(j)); j++; progress = j; } } catch (NullReferenceException ex) { t[i] = Task.Run(() =&gt; FindWIN(j)); j++; progress = j; } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload