views:

92

answers:

4

hi, I have 2 forms ParentForm and a child form. In my parent form I have a thread listener which listens to a feed which updates an area of the ParentForm. Now, I have a ChildForm which also needs the data from the listener to be placed on an area of the ChildForm. the thread listener uses delegate to update my ParentForm when it gets a feed.

My ParentForm has these.

private delegate void UpdateLogCallback(string strFeed);
private Thread thr;

private void InitializeFeed()
{
    ...
    // Get the feed connection
    ...
    thr = new Thread(new ThreadStart(ReceivedFeeds));
    thr.Start();
}

private void ReceivedFeeds()
{
    string strFeed = GetFromStream();

    // invoke my updater while connected
    while(Connected)
    {
        this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { strFeed });
    }
}

private void UpdateLog(string strFeed)
{
    txtLog.AppendText(strFeed + "\r\n");
}

this works fine, now here's the question. When I open a ChildForm from the ParentForm, I also want to update a centain part of that form using what I get from the ReceivedFeeds() in my ParentForm how will I achieve this? I can't create another feed connection in the ChildForm since this will duplicate the Connection and cause an error. I just want to do the same as what UpdateLog() will do in the ChildForm.

Edit

I'm calling the ChildForm to open on an OnClick event on the parent form and show it.

// onclick event
ChildForm childForm = new ChildForm();
childForm.Name = ((ListBox)sender).SelectedItem.ToString();
childForm.ShowDialog(this);

This is how I open my ChildForm and how do I call the methods inside the ChildForm in my UpdateLogCallback or in my UpdateLog()

I also have an UpdateLog() method in my ChildForm.

A: 

Either Invoke two methods instead of one, or call the childform update method from within the parentform update method, -best in my opinion- add an event to the parentform that gets raised from within the UpdateLogCallback on the parentform. Any classes aware of the parentform can thus piggy back along on the whole update system.

David Rutten
hi thanks for the reply, I have updated my question. How do I achieve your suggestion given the addition information above?
rob waminal
A: 

Either sharing one of method from parent and calling it from child or sharing one method from child and calling it from parent may solve it

KoolKabin
A: 

set form invisible on creation. write following in ctor (just after InitializeComponents()):

Visible = false;

now add a method in child form like this:

internal void ShowForm(Form parent)
{
    var p = parent as Form1;
    if (p != null)
    {
        p.DataReady += (s, ev) =>
        {
            //update your data here
        };
    }
    ShowDialog(parent);
}

in parent form declare an event like this:

internal event EventHandler<EventArgs> DataReady;

depending on your need to pass data to child form, you may need to create custom class handling form EventArgs. Show the child form like this:

var f=new ChildForm();
f.ShowForm(this);//this is parent form
TheVillageIdiot
+1  A: 

If you simply hold a reference to the child form from within your parnet form you can call the UpdateLog method from the parent form

private void UpdateLog(string strFeed)
{
    txtLog.AppendText(strFeed + "\r\n");
    _child.UpdateLog(strFeed);
}

EDIT:

Also, if you have many child forms you can have a collection of them. Just make sure you remove them from the list when they close.

aqwert
thanks, I have achieved it like this way. but if I have more than 1 reference of my ChildForm, like they can be opened on the same time and I need to update a specific ChildForm. How will I look for them in the Form Collection?
rob waminal
Depends on the matching condition you have. Probably be better to have a check in the child form UpdateLog method to determine that as it would be better encapsulation than having the parent form figure that out
aqwert