none
How to access a parent thread method from the child thread ?

    Question

  • I have a windows Parent Form that invokes child form threads.  Each child form does some processing and when its done, the child form needs to access a method on the parent form.

    Can some one tell me how can I accomplish this ? I am trying this for the first time and didnt see any examples on the net.

    Any help or pointers to an example source code will be much appreciated.

    Thanks.
    Monday, September 15, 2008 4:24 AM

All replies

  • You can use composite controls and try it.
    public class mycontrol:usercontrol
    {
    //code

    you can add methods,properties and events to this .
    Depending upon what u want to do with it ,it varies.
    Monday, September 15, 2008 6:45 PM
  • create a delegate for parent thread method and in child thread simply invoke that delegate.

    Hope it will help you.

    Regards,
    Haider Raza Software Engineer, ZABSolutions.
    Monday, September 15, 2008 7:05 PM
  • hi,

    Do you use like form2.show() to invoke child form? If so , i think you can pass the reference of the parent form when invoking the child form form2.show(this), and you can call the public method in the parent form.

    Assume form1 is the parent form ,form2 is the child form.
    In form1.cs :
    private void Form1_Load(object sender, EventArgs e)  
            {  
                Form2 f = new Form2();  
                f.Show(this);  
                 
            }  
            public void amethod()  
            {  
                MessageBox.Show("from 1");  
            } 
    In form2.cs:
    private void Form2_Load(object sender, EventArgs e)  
            {  
                ((Form1)this.Owner).amethod();  
            } 

    hope that help.

    regards,

    harry
    Tuesday, September 16, 2008 9:10 AM
  • Thanks guys for the suggestions.

    On child thread Im spawning client created dynamic assemblies. My client doesnt need to know anything about parent implementation.  

    Harry - This is in regards to threads (parents and child) not forms exactly although my child threads do invoke assemblies of type Forms.
     
    Haider & Sethluri, let me try out your suggestions but it seems even using the delegates I still have to reference parent assembly.

    So my original question still stands.
    Tuesday, September 16, 2008 2:23 PM
  • hi,

    As I learnt from your post ,you are trying to access a method on the parent form. I think we can just call it as it is public . The following code snippet works:



    namespace invokes_child_form_threads  
    {  
        public partial class Form1 : Form  
        {  
            private Thread athread = null;  
            public Form1()  
            {              
                InitializeComponent();  
            }  
            private void Form1_Load(object sender, EventArgs e)  
            {  
                this.athread = new Thread(new ThreadStart(startthread));  
                athread.Start();          
            }  
            public void sayhi()  
            {  
                MessageBox.Show("hi");  
            }  
            private void startthread()  
            {              
                Form3 f1 = new Form3();             
                this.Adding(f1);   
            }  
            public void Adding(Form3 child)  
            {  
                if (this.InvokeRequired)  
                {  
                    AddChild f = new AddChild(this.Adding);  
                    this.Invoke(f, new object[]{child});                  
                }  
                else  
                {               
                    child.Show(this);  
                }  
            }  
            public delegate void AddChild(Form3 child);  
        }  
        public class Form3 : Form  
        {  
            public Form3()  
            {  
                Form1 f = Application.OpenForms[0] as Form1;  
                f.sayhi();  
            }         
        }     
    }  
     


    Could you also share with us that What effect would you like to get and some code?maybe
    some community members have some idea on it.

    Best regards,
    Harry
    Thursday, September 18, 2008 9:32 AM