0

I have an app where multiple child forms are spawned from a parent form, using a single child form definintion (i.e. frmParent spawns multiple instances of frmChild).

Each child runs in it's own thread - I want to be able to interact with each child independently (and I don't like MDI).

So - if the parent form is closed, how do I instruct the child forms to close themselves?

        private void btn1_Click(object sender, EventArgs e)
    {
        Thread thread = new Thread(delegate ()
        {
            using (Child child = new Child())
            {
                child.MyName = "1";
                child.ShowDialog();
            }
        });
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
    }

    private void btn2_Click(object sender, EventArgs e)
    {
        Thread thread = new Thread(delegate ()
        {
            using (Child child = new Child())
            {
                child.MyName = "2";
                child.ShowDialog();
            }
        });
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
    }
Dave Tapson
  • 810
  • 1
  • 9
  • 22
  • 4
    All UI components should be running on just one thread: the main (UI) thread. That's the rule. – rory.ap Sep 29 '16 at 12:40
  • 1
    Are you sure they're in their own threads? Usually there's only one UI thread. How are you showing them? – Sami Kuhmonen Sep 29 '16 at 12:41
  • So how do you get multiple forms running independently if you don't want to use MDI if you don't use multiple threads? – Dave Tapson Sep 29 '16 at 12:41
  • @SamiKuhmonen Code added to question – Dave Tapson Sep 29 '16 at 12:42
  • 3
    Just use `child.Show()`. You don't need/should not use another thread. – Reza Aghaei Sep 29 '16 at 12:43
  • @RezaAghaei child form disappears if I use child.Show() - and can I spawn multiple instances of child? – Dave Tapson Sep 29 '16 at 12:45
  • 1
    Without using another thread in above click event handlers, just write: `new Child().Show();` – Reza Aghaei Sep 29 '16 at 12:50
  • @RezaAghaei Yep, thanks. – Dave Tapson Sep 29 '16 at 12:53
  • 1
    It's better to edit the answer and put a conclusion based on comments, like *As a concolution based on comments, all UI components should be running on just one thread: the main (UI) thread. That's the rule. You don't need/should not use another thread. So without using another thread in click event handlers, just write: `new Child().Show();`*. It's a more a complete and useful answer. You will have my vote. – Reza Aghaei Sep 29 '16 at 13:03
  • Done as suggested :) – Dave Tapson Sep 29 '16 at 14:33
  • 1
    Good job! Also read the linked post about [Do I need to Dispose a Form after the Form got Closed?](http://stackoverflow.com/a/39501121/3110834). – Reza Aghaei Sep 29 '16 at 16:24

1 Answers1

2

As a conclusion based on comments, all UI components should be running on just one thread: the main (UI) thread. That's the rule. You don't need/should not use another thread. So without using another thread in click event handlers, just write: new Child().Show();

Dave Tapson
  • 810
  • 1
  • 9
  • 22
  • Do not write `new Child().Show()` as `Child` is disposable. It's a memory leak this way. – Enigmativity Sep 29 '16 at 14:37
  • So what IS the solution then? – Dave Tapson Sep 29 '16 at 14:40
  • 1
    @Enigmativity That doesn't leak. The form will be added to the Application.OpenForms collection and will be disposed of by the user or by the program when the user closes the main form. – LarsTech Sep 29 '16 at 15:14
  • 1
    [Do I need to Dispose a Form after the Form got Closed?](http://stackoverflow.com/a/39501121/3110834) – Reza Aghaei Sep 29 '16 at 15:39
  • @Enigmativity When you show a form using `Show` method you don't need to (and you can't) call Dispose. The form will be disposed itself after got closed. – Reza Aghaei Sep 29 '16 at 15:41