This may seem like a somewhat contrived example, but I'm left scratching my head.
Ok, I have a console app that instantiates a WindowsForm and calls a method called DoSomeWork() on the form.
class Program
{
static void Main(string[] args)
{
Form1 form = new Form1();
form.DoSomeWork();
}
}
Form1 looks like this...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void DoSomeWork()
{
OuterClass outerClass = new OuterClass();
outerClass.DoSomeWork();
}
}
Outer class, in turn, looks like this...
public class OuterClass
{
public void DoSomeWork()
{
InnerClass innerClass = new InnerClass();
innerClass.DoSomeWork();
}
}
And finally InnerClass looks like this...
public class InnerClass
{
private BackgroundWorker _backgroundWorker = new BackgroundWorker();
public InnerClass()
{
_backgroundWorker.WorkerReportsProgress = true;
_backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundWorker_DoWork);
_backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
}
void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int i = 0; //I've placed a break point here. But it's never hit
}
void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
worker.ReportProgress(42);
}
public void DoSomeWork()
{
_backgroundWorker.RunWorkerAsync();
}
}
For a reason unknown (to me), the BacgroundWorker in InnerClass never seems to fire the ProgressChanged event. If I replace
Form1 form = new Form1();
with
OuterClass outerClass = new OuterClass()
in class Program, it works fine. So why is that my events don't fire when I'm calling the same methods through a Form?
Thanks!
EDIT: I seemed to be throwing people off by leaving the ProgressChanged event handler as throwing a NotImplementedException, so I've removed it for clarity.