Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Backround Worker In Different Namespace/Class WPF
    primarykey
    data
    text
    <p><strong>EDIT: Solved by adding missing progress changed event handler.</strong></p> <p>I'm porting a WPF VB.net application to C# and am having an issue with a background worker that has a DoWork method in a different class. I have a suspicion that I am not casting the background worker correctly or I may need a handler?</p> <p>The vb sample code works fine and the progress bar indicates properly, the C# code seems like it fires the bw.RunWorkerAsync(); method as it reports as IsBusy=True but there is no other response, progress or calls to the external class (as far as I can tell).</p> <p>This is a WPF application and the issue is regarding a Usercontrol trying to report progress from a public class outside of the control.</p> <p>If someone could point me in the right direction I would really appreciate it.</p> <p>The C# sample code</p> <pre><code> namespace testApp.Usercontrols public partial class ucHome : UserControl { public Sharing.clsDownloadCollection foo = new Sharing.clsDownloadCollection(); BackgroundWorker bw = new BackgroundWorker(); public ucHome() { InitializeComponent(); } private void Button1_Click(object sender, RoutedEventArgs e) { bw.WorkerReportsProgress = true; pBar.Maximum = 50000; //progress bar on ucHome pBar.Value = 0; **// Indicates IsBusy=True when debugging but can't see any further activity bw.RunWorkerAsync(); //** } public void bw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; var foo=new foo(); // possible problem? foo.DoWork((BackgroundWorker)sender); } public void bw_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) { this.pBar.Value = e.ProgressPercentage; } </code></pre> <p>The external class different project folder/namespace</p> <pre><code> namespace testApp.Sharing { //different namespace /folder than ucHome public class foo { public void DoWork(BackgroundWorker bw) { for (int i = 0; i &lt;= 50000; i++) { i += 1; bw.ReportProgress(i - 1); } } } </code></pre> <p>The sample VB code that works as expected.</p> <pre><code> Public Class ucHome 'user control Public foo As New foo Friend WithEvents bw As New BackgroundWorker Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click bw.WorkerReportsProgress = True pBar.Maximum = 50000 pBar.Value = 0 bw.RunWorkerAsync() End Sub Private Sub bw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bw.DoWork 'possible my C# is not casting correctly? foo.DoWork(DirectCast(sender, BackgroundWorker)) End Sub Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bw.ProgressChanged Me.pBar.Value = e.ProgressPercentage End Sub End Class </code></pre> <p>the class foo that user control calls</p> <pre><code>Public Class foo Public Sub DoWork(ByVal bw As BackgroundWorker) For i As Integer = 0 To 50000 i += 1 bw.ReportProgress(i - 1) Next End Sub End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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