Question

Need to make an addition to a program that is currently in production and I can't really do much architecture changes.

Main Thread (T1) -> GUI Elements Other Threads (T2...) -> Calculation Elements (and outside events).

There is an object class Foo that is running on T2. Foos are created based on various outside events.

I need to add a DataGrid to a new form that will simply display the properties of different Foo objects.

Foo implements INotifyPropertyChanged

winform GridForm has a BindingList<Foo> FooList object. GridView's data is bound to it.

Obviously I am making a x-thread call when I call NotifyPropertyChanged in my Foo and that doesnt work.

I cannot spawn my Foo objects from the main thread, nor i can make the form on Thread2.

Im scratching my head here on how to invoke the main thread.

I do have a static class Foo-Brain that has a reference to the main form (and thus I can get the GUI thread.). The question is how to tie this all together.

Any ideas?

(I was thinking about duplicating Foo objects and running them on the GUI thread and have the FooList containt those, but that seems way to hacky and inefficient).

Thanks.

PS. Hope everyone is alright with Sandy.

EDIT: Sample Code:

class Person : INotifyPropertyChanged // this is the object that will be updated from a
     //different thread; It will have the reference to the main form (GUI)
{
    public event PropertyChangedEventHandler PropertyChanged;
    string comments;  
    private Form1 mainForm = null;

    private void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged == null) return;
        PropertyChanged(this, new PropertyChangedEventArgs(name));         
    }


    public Person(string comments, Form1 f) 
    {
        this.mainForm = f;
        this.comments = comments;
    }

    public string Comments 
    {
        get { return comments; }
        set
        {   comments = value;
            NotifyPropertyChanged("Comments");
        }
    }
}

The code from GUI form:

private void Form1_Load(object sender, EventArgs e)
{
    gridControl.DataSource = gridDataList;
}

private void runmethread()
{
    Thread t = new Thread(anotherthread);
    t.Start();
}

private void anotherthread()
{
    gridDataList[0].Comments = "NEW THREAD";
}

private void button1_Click(object sender, EventArgs e)
{
    runmethread();
}

private void button2_Click(object sender, EventArgs e)
{
    gridDataList[0].Comments = "OLD THREAD";
}

obviously button1_click with cause the x-thread problem (what I am trying to solve)

Was it helpful?

Solution

I think this is what you are asking ...

Change your INotifyPropertyChanged implementation in Foo to:

public class Foo: INotifyPropertyChanged
{

  public event PropertyChangedEventHandler PropertyChanged;

  private void NotifyPropertyChanged(String propertyName = "")
  {
      if (PropertyChanged != null)
      {
       if (mainForm.InvokeRequired)
       {
           mainForm.Invoke((MethodInvoker) delegate
           {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
           });
        }
        else
        {
           PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
        }
      }
  } 
}

This will make sure that whenever a PropertyChanged event occurs (and for its entire duration while its running), it will be the only thing that T1 will run. So the PropertyChanged can be raised from both threads no problem.

related article you may find educational or confusing:) there is no middle ground:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top