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 6876367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:28:06+00:00 2026-05-27T04:28:06+00:00

We have a Windows Forms application that contains thousands of forms. Many of these

  • 0

We have a Windows Forms application that contains thousands of forms.

Many of these are temporarily displayed as dialogs via the ShowDialog() method.

This application has been around for years and we’ve discovered that many of the forms are not getting garbage collected in a timely manner due to various resource leaks in the form or the controls that it uses.

Specifically, we’ve found examples of GDI+ resources that aren’t being disposed of properly, although there may be other types of resource leaks that have not yet been characterized.

Although the right way to resolve this is obviously to go through every form and every control and eliminate all of the resource problems. This will take some time to accomplish.

As an short term alternative, we have found that explicitly calling Dispose() on the form seems to initiate the garbage collection process and the form and its resources are deallocated immediately.

My question is whether is would be a reasonable workaround to wrap each form’s ShowDialog() block in a using statement so that Dispose() is called after the form has been displayed, and also would this be a good practice to institute in general?

For example, change the existing code from this:

public void ShowMyForm()
{
    MyForm myForm = new MyForm();
    myForm.ShowDialog();
}

To this:

public void ShowMyForm()
{
    using (MyForm myForm = new MyForm())
    {
        myForm.ShowDialog();
    }
}

In our testing, MyForm’s Dispose() method never gets called for the first example, but it gets called immediately for the second example.

Does this seem like a reasonable approach as a short term workaround while we spend the time tracking down each of the specific resource issues?

Are there other approaches that we could consider for a short term workaround and/or methodologies for identifying and resolving these types of resource issues?

  • 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-27T04:28:07+00:00Added an answer on May 27, 2026 at 4:28 am

    According to MSDN, you must explicitly call Dispose on forms shown using ShowDialog (unlike with the Show method):

    When a form is displayed as a modal dialog box, clicking the Close
    button (the button with an X at the upper-right corner of the form)
    causes the form to be hidden and the DialogResult property to be set
    to DialogResult.Cancel. Unlike non-modal forms, the Close method is
    not called by the .NET Framework when the user clicks the close form
    button of a dialog box or sets the value of the DialogResult property.
    Instead the form is hidden and can be shown again without creating a
    new instance of the dialog box. Because a form displayed as a dialog
    box is hidden instead of closed, you must call the Dispose method of
    the form when the form is no longer needed by your application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a solution that contains both a Windows Forms application and a Console
I have a .NET application that contains a checkbox (System.Windows.Forms.Checkbox). This component (WindowsForms10.BUTTON.app.0.378734a1) is
I have a Windows forms application that contains per-user settings. I'm using Properties.Settings.Default.Upgrade() to
In my C# Windows Forms application, I have a user control that contains other
Background : I have a C# Windows Forms application that contains a Windows service
I have a C# Windows Form application that contains a menu with this event:
I have a windows forms application that reads and updates an XML file with
I have a Windows Forms application that on it I have a RichTextBox, like
I have a Windows Forms application that uses some Application.Idle handlers to change the
If you have a windows service and a windows forms application that uses the

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.