Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Thread does not exit when a form is closed while other forms are hidden

The following code successfully calls another form, Shows it, hides the first one and repeats the same process for Form2. (There are 3 forms in total).
But if someone closes the app using the X in the middle of the application. (In Form2 or Form3), the application keeps running in the background and I have to manually terminate it using the task manager.

I am a student so my code is probably fairly unoptimized, but despite looking for an answer on the internet I could not find a solution.

using System;
using System.Windows.Forms;
using System.Diagnostics;

namespace FormSurveyOpdracht
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void btnNext_Click(object sender, EventArgs e)  //When Next button is clicked:
        {
           //Rest of the Code

            Form2 form2 = new Form2(fullname);                  //Define a new form Form2, and send the variable fullname.
            form2.Show();                                       //Show the Form2.
            this.Hide();                                        //Hide this Form (Form1)
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e) //if the form is closed in an unexpected way, exit the whole application.
        {
            Application.Exit();
        }
    }
}

What I tried: I tried to close the application using the X.
I was expecting: The whole application to close.
What happened: The application kept running in the background.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What else I tried: I tried putting a

   private void Form1_FormClosing(object sender, FormClosingEventArgs e) //if the form is closed in an unexpected way, exit the whole application.
   {
       Application.Exit();
   }

on each form (Form1 for form1, Form2 for form2 etc.)
What happened: The application keeps running.
I also tried: Application.ExitThread(); and Environment.Exit(0); Does not work.

>Solution :

You will have to bind an event on every form you open. Make sure you don’t try to bind this event to the Type Form2, you cannot bind events on non instances of objects. You will need to bind it to form2 the instance that you have made using the = new Form2()

using System;
using System.Windows.Forms;
using System.Diagnostics;

namespace FormSurveyOpdracht
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void btnNext_Click(object sender, EventArgs e)  //When Next button is clicked:
        {
           //Rest of the Code

            Form2 form2 = new Form2(fullname);                  //Define a new form Form2, and send the variable fullname.
            form2.Closed += (sender, e) => this.Close(); //note that you bind it to the instance of `Form2` rather than the Type
            form2.Show();                                       //Show the Form2.
            this.Hide();                                        //Hide this Form (Form1)
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e) //if the form is closed in an unexpected way, exit the whole application.
        {
            Application.Exit();
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading