-3

In my Windows form the application doesn't exit even though I execute this.Close();

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

If I try disposing it directly it just crashes.

In my main class I open an instance, which is what i'm trying to close. Before it didnt't want to close, it didn't open.

This is not my main class.

FIX: Replacing "show()" with "showDialog()" fixed it.

user3902017
  • 315
  • 3
  • 12
  • 1
    Any threads, any calls to ProcessMessages ? Anything else we might like to know? – H H Aug 10 '14 at 10:10
  • My first guess: You have hooked up `FormClosing` and set `e.Cancel` to true. – Sriram Sakthivel Aug 10 '14 at 10:10
  • read this http://stackoverflow.com/questions/13046019/winforms-application-exit-vs-enviroment-exit-vs-form-close –  Aug 10 '14 at 10:12
  • there must be other code responsible for this, try to reproduce it in minimal code. Did you open more Forms? Did you override/hook FormClosing? – firda Aug 10 '14 at 10:17
  • Read the `0xA3's` answer from this question http://stackoverflow.com/questions/3912836/c-sharp-why-does-form-close-not-close-the-form – Earth Aug 10 '14 at 10:19
  • 3
    @user3902017 - edit your question to clarify or it will get closed. – H H Aug 10 '14 at 10:23
  • 1
    You should use Application.Exit() instead http://stackoverflow.com/questions/12977924/how-to-properly-exit-a-c-sharp-application – ArtemKunik Aug 10 '14 at 10:42

2 Answers2

1

Remarks from Form.Close()

When a form is closed, all resources created within the object are closed and the form is disposed. You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler. If the form you are closing is the startup form of your application, your application ends.

The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection. NoteNote

When the Close method is called on a Form displayed as a modeless window, you cannot call the Show method to make the form visible, because the form's resources have already been released. To hide a form and then make it visible, use the Control.Hide method. Caution noteCaution

Prior to the .NET Framework 2.0, the Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

This clearly states that the form should close and application should end unless you do something else we cannot see in your example.

firda
  • 3,268
  • 17
  • 30
-1

The Close method is a Form's method, it closes the Form, this closes the Application instead :

// Close everything down.
Application.Exit();

As you can see the Application Class doesn't have a Close method, it has an Exit method instead, the this.Close() closes the concerned Form, but keeps the application running.

AymenDaoudi
  • 7,811
  • 9
  • 52
  • 84
  • Op's title says Form doesn't close. Why would that happen? `Close` should close the form unless you set `e.Cancel` in `FormClosing` handler – Sriram Sakthivel Aug 10 '14 at 10:14
  • 1
    @SriramSakthivel : he actually says : `... the application doesn't exit even though I execute this.Close();` he wants to exit the app, through closing the Form – AymenDaoudi Aug 10 '14 at 10:16
  • Agreed, question is not clear. Title says one and content say another – Sriram Sakthivel Aug 10 '14 at 10:17
  • 4
    Closing the MainForm should automatically terminate the process. – H H Aug 10 '14 at 10:19
  • @HenkHolterman You're right. I downvoted this answer and later removed it. Because, we don't know whether it is the MainForm. – Sriram Sakthivel Aug 10 '14 at 10:21
  • I'm willing to assume that exitButton_Click is a menu item on the MainForm. Something else is at play, the OP should clarify. – H H Aug 10 '14 at 10:23
  • 2
    @HenkHolterman : it is not mentioned that it s about the Main Form, the OP seems to want to exit the app – AymenDaoudi Aug 10 '14 at 10:23
  • 1
    Wait, Even though it is the MainForm, `Application.Exit` will not force the foreground threads to exit which will result in keeping the process alive. So technically this answer is incorrect. – Sriram Sakthivel Aug 10 '14 at 10:25
  • 1
    @SriramSakthivel that's a special case, the OP wants simply to close his app, I gave him a correction to what he tried, according to MSDN : `Exit() : Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.` – AymenDaoudi Aug 10 '14 at 10:31