The requirement is to pass a ListBox data from the Business Logic Class to the Main UI.
Under my main class (Windows Form1), you will find the "backgroundWorker DoWork" and the Delegate methods:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Code.BusinessLogic bl = new Code.BusinessLogic();
bl.Process(backgroundWorker1);
}
public delegate void AddListBoxItemDelegate(object item);
public void AddListBoxItem(object item)
{
if (this.listBox1.InvokeRequired)
{
// This is a worker thread so delegate the task.
this.listBox1.Invoke(new AddListBoxItemDelegate(this.AddListBoxItem), item);
}
else
{
// This is the UI thread so perform the task.
this.listBox1.Items.Add(item);
}
}
Under the Business Logic class, you will find the Process method.
public void Process(BackgroundWorker bg)
{
for (int i = 1; i <= 100; i++)
{
AddListBoxItem("Test");
Thread.Sleep(100);
bg.WorkerReportsProgress = true;
bg.ReportProgress(i);
}
}
In this scenario, the AddListBoxItem (from the Process Method) is not adding items to the Main UI.