EDIT: Solved by adding missing progress changed event handler.
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?
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).
This is a WPF application and the issue is regarding a Usercontrol trying to report progress from a public class outside of the control.
If someone could point me in the right direction I would really appreciate it.
The C# sample 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;
}
The external class different project folder/namespace
namespace testApp.Sharing
{
//different namespace /folder than ucHome
public class foo
{
public void DoWork(BackgroundWorker bw)
{
for (int i = 0; i <= 50000; i++) {
i += 1;
bw.ReportProgress(i - 1);
}
}
}
The sample VB code that works as expected.
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
the class foo that user control calls
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
Solved by adding missing backgrounder ProgressChanged event handler.