-1

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
fmourtaza
  • 179
  • 1
  • 16
  • 1
    Where is the code you instantiate the `BackgroundWorker`? – Patrick Hofman Sep 18 '14 at 13:52
  • 1
    Ugh, you need to delete this code and read [BackgroundWorker specification which comes with sample code](http://msdn.microsoft.com/pl-pl/library/system.componentmodel.backgroundworker(v=vs.110).aspx) before writing it again. – PTwr Sep 18 '14 at 13:56
  • You just shouldn't be using a BGW at all. To do some UI action periodically after a set interval of time you should be using a `Timer`, not a BGW. – Servy Sep 18 '14 at 14:15

1 Answers1

0

The following setup works fine for me.

However: As pointed out in previous comments I'm not quite sure what you're trying to archieve here was in the designers mind. Handing the background worker over to a class which then needs to reference back the form the worker originated from, to me, seems overly complicated and not exactly in line with coding concepts i.e. data encapsulation.

Consider rewriting the code to have the backgroundworker poll the data required to do its job from the business logic rather than give it to the business logic and have it work in there.

In the Main Window

    public Form1()
    {
        InitializeComponent();

        backgroundWorker1.RunWorkerAsync();
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
    }
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Class1 bl = new Class1();
        bl.f = this;
        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);
        }
    }

In your businesslogic class

class Class1
{
    public Form1 f { get; set; }
    public void Process(BackgroundWorker bg)
    {
        for (int i = 1; i <= 100; i++)
        {
            f.AddListBoxItem("Test");
            Thread.Sleep(100);
            bg.WorkerReportsProgress = true;
            bg.ReportProgress(i);
        }
    }
}
Spraygun
  • 26
  • 1