Thalia
Thalia

Reputation: 14615

C# form closing at program end

I have a windows forms question:

Program.cs:

Application.Run(new frmStart());

frmStart: on btnLoad_Click,

frmLoad formLoad = new frmLoad();
formLoad.Show();
this.Hide();   // if I do a this.Close();  after it shuts down and doesn't get to show the form

frmLoad: on btnCancel_Click:

Application.Exit();   
// or this.Close(); 
// or even: base.Close();

The form disappears but the program doesn't end, I still have to press the blue "Stop Debugging" to make it stop.

I have been looking... I know it is possible to make the program really stop, and not just freeze when you close the second form, even if you don't keep the first form on the screen, but can't remember and can't figure out how.

Upvotes: 0

Views: 2390

Answers (5)

Steve Py
Steve Py

Reputation: 34978

Ack, -1 on Application.ExitThread!

The issue is that you haven't closed the main form. The simplest way is to hook onto the 2nd form's Closed event and have it close the main form. For example the code to open the 2nd form changes to:

        var newForm = new frmLoad();
        newForm.FormClosed += (closedSender, closedE) => Close();
        newForm.Show();
        Hide();

This essentially sets up so that when the frmLoad form closes, the main form calls it's Close() method. I used a Lambda expression for the event handler, but you can just as easily create a private method accepting an (object sender, EventArgs e) and point .FormClosed at it.

*Edit: Sorry, missed that you only want to close on certain state. In which case on your frmLoad, create a public property such as:

public bool UserCancelled { get; private set; }

where the Cancel button sets this to True before closing the form. Your event handler in the main form changes to:

        var newForm = new frmLoad();
        newForm.FormClosed += (closedSender, closedE) =>
                                {
                                    if (newForm.UserCancelled)
                                        Close();
                                };
        newForm.Show();
        Hide();

Upvotes: 1

Matthew Sanford
Matthew Sanford

Reputation: 1099

I vote yuck on all the answers. Override Form Closing in your primary form and close the secondary form first.

Upvotes: 1

HatSoft
HatSoft

Reputation: 11201

Please use the Application.ExitThread() method, the method exits the message loop on the current thread and closes all windows on the thread.

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.exitthread

Upvotes: 1

David Thielen
David Thielen

Reputation: 33016

You really should call Close() on both. That's the only clean way as otherwise the first form never is told to close down and doesn't clean up.

You may know that it's safe to do this, but someone else working on the code later may add code in the OnClose in the first form that they need called. They will say not nice things about you when they finally figure out why their code is not called.

If you close both, then your app will exit.

Upvotes: 1

Chuck Savage
Chuck Savage

Reputation: 11955

In frmStart add:

public static frmStart Current;

Then in the constructor add:

Current = this;

Then in frmLoad: on btnCancel_Click:

frmStart.Current.Close();

Upvotes: 1

Related Questions