Always Get Better

C#: Form.Close() vs Form.Dispose()

When working with a Windows GUI, it may seem unclear whether to use Form.Close() or Form.Dispose() to get rid of a dialog at runtime.

Form.Close() removes the dialog from sight and calls the Closing() and Closed() methods.  You can still access the form and bring it back later on.

Form.Dispose() destroys the dialog and frees its resources back to the operating system.   It does not call the form’s Closing() and Closed() methods. Once disposed, you may not recall a form.

Which to use? If you have no logic in the form’s close methods and don’t intend to re-use the form, go with Dispose().  Otherwise, go with Close().  Some programmers aren’t sure which to use, and they use both – Close() then Dispose()!

Tags: ,

9 Responses to “C#: Form.Close() vs Form.Dispose()”

  1. NPT says:

    “Form.Dispose() destroys the dialog and frees its resources back to the operating system. It does not call the form’s Closing() and Closed() methods. Once disposed, you may not recall a form.”

    It certainly does Close the form. When disposing a form, it releases all resources that its using, including the resources keeping the form on the screen. Here’s an example…

    using(Form f = new Form())
    {
    f.Show();

    // Do some stuff
    }

    This will close AND dispose the form without ever calling Close.

  2. mwilson says:

    Yes – Form.Dispose() does close the form. But it doesn’t call the Closing and Closed methods. If you have any code logic triggered by the form being closed, it will NOT be executed when the form is Disposed, unless you make a call to Close() first. Otherwise the form is just silently taken away.

  3. James says:

    In this case it makes sense to call both. Or call .Close() within a using construct. It allows the most graceful shutdown for a form, frees up resources right away, and suppresses the finalizer.

  4. lellis says:

    “Which to use? If you have no logic in the form’s close methods and don’t intend to re-use the form, go with Dispose(). Otherwise, go with Close(). Some programmers aren’t sure which to use, and they use both – Close() then Dispose()”

    Actually, sometimes it is better to call dispose IF you have logic in the close event! For example, I have a form with logic in the closing event.
    The logic just cancels the close. At some point, I *really* want to close the form, and the easiest way to do it is to call the dispose method. This
    way I don’t need special logic in the closing event to deal with the special case.

  5. xpin says:

    I read MSDN and find this text:
    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.

    But it is false.
    1)I create form without showing.
    2)I delegate more methods of this form for more classes.
    3)I make FormClosing event in designcode and write e.cancel = true;
    4)I make FormClosed event in designcode and exclude all delegate metods.
    5)After create form, I write form.Close(); (Certain, since there is event of the cancelling).
    6)I make show on click button! – Has exception, because form is disposed!

    This is result of “Handle==0”. When property Handle don’t read and not created,
    my events FormClosed and FormClosing is not call. And I have more problems and bag.

    I write code after create of form:
    if (form.Handle != (IntPtr)0) form.Tag = 0;
    – has 0 bag and 0 problem.

    Since this bug .NET or lying MSDN

    best regards,
    xpin

  6. Quadko says:

    if I understand that last problem posted by xpin, I think the issue isn’t MSDN or a bug, exactly, but that the form was never shown in the first place.

    That Handle==0 issue is a common bug resulting from delayed acquiring of a window handle from the OS. If you don’t show the Form, the handle is never created – same state as after Dispose when it is released. That code to reference form.Handle is the common workaround to force the handle to be created. This isn’t really related to Close() verses Dispose(), just is related to Forms.

    And btw, form.Close() balances form.Show(), the way file.Close() balances file.Open().

  7. Form.Close() removes the dialog from sight and calls the Closing() and Closed() methods. You can still access the form and bring it back later on. says:

    Form.Close() removes the dialog from sight and calls the Closing() and Closed() methods. You can still access the form and bring it back later on.

  8. Mankuji says:

    Application.Exit() :
    The Exit method stops all running message loops on all threads and closes all windows of the application. This method does not necessarily force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call ExitThread. When calling Application.Exit everything is told to stop immediately and then the application is shutdown. Resulting in events like Form.Closed and Form.Closing not are being fired.
    for full implementation of Application.Exit(), this.Close() and this .Dispose()
    check here: http://www.mindstick.com/blog/196/Application%20Exit%20this%20Close%20and%20this%20Dispose

  9. exilon says:

    when i call Form.Dispose() it closes the whole application, not only Form.
    I have this in Form :
    protected override void Dispose(bool disposing)
    {
    if (disposing && (components != null))
    {
    components.Dispose();
    }
    base.Dispose(disposing);
    }

Leave a Reply