Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5935093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:12:18+00:00 2026-05-22T15:12:18+00:00

I am trying to close my main(the parent) form when a child form is

  • 0

I am trying to close my main(the parent) form when a child form is being closed. However this gives me a StackOverflow exception.

However if I call _child.Dispose on the FormClosed event it works as intended. Should I do this? Why should I call Dispose? (because of the .Show() it shouldn’t be neceserry right?

A small demo:

public partial class frmChild : Form
{
    public frmChild()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

public partial class frmParent : Form
{
    private frmChild _child;

    public frmParent()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _child = new frmChild();
        _child.FormClosed += child_FormClosed;
        _child.Show(this);
    }

    void child_FormClosed(object sender, FormClosedEventArgs e)
    {
        //_child.Dispose(); <-- uncomment and it works
        this.Close(); // <-- StackOverflow exception
    }
}

The solution, commented by Teoman Soygul (for future reference):

Closing the main form with
this.Close(); signals all child
windows to close in order so that
creates the infinite loop

After calling this.Close() in the parent it will signal all children to Close aswel, which will send another FormClosed event…
I solved it by not specifieing the owner in _child.Show(); I didn’t use the owner anyway.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:12:19+00:00Added an answer on May 22, 2026 at 3:12 pm

    Since every time you call this.Close(); the FormClosed event gets fired which then calls this.Close(); again, you create an infinite loop. On the other hand, if the form is already disposed (as in you uncomment the dispose line), the FormClosed event does not get fired again as the object is already disposed. So disposing of the form on the event is right, or if you don’t want to do that, you can add an additional check with a private bool field like:

    if (!formClosed)
    {
      this.formClosed = true;
      this.Close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to close a child window with JavaScript, and in Firefox everything works
I'm trying to create a two-way communication between parent and child processes using 2
I am trying to create a child that calls sort. The parent sends data
I'm trying to redirect the stdin from the parent to the child through the
I'm trying to close FancyBox from within the iframe, but parent.$ is always undefined
Trying to close all forms except for the main menu using FormCollection formsList =
I am trying to close a memcached connection to start a new one. But
I'm trying to close the distance between the checkboxes and the labels. I would
I am wondering how I can prevent a crash when trying to close UIDoc
Possible Duplicate: Cross-thread operation not valid I am trying to close the base of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.