4

My c# windows forms application has 5 forms which I am displaying one after the another. When the user clicks on next button, the code I have given is:

new Form1().Show();
this.Hide();

However I do not want my current Form to hide. I want to close it/dispose it so that it does not consume memory. I want to release its resources like the images and variable used as soon as I am done with it. For that I tried implementing:

new Form1().Show();
this.Close();    //Form 2

but this simply closes both the forms. I even tried swapping the positions of the above two lines:

this.Close();
new Form1().Show();

but this also does same thing.

How do I release the resources of one form as soon as I am done with it? because my program throws out of memory exception when I try to re-open my Form 2 using:

new Form2().Show();
this.Hide();
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
Chuker
  • 125
  • 1
  • 3
  • 16
  • 4
    If you close your main form, your app will close... – L.B Oct 18 '14 at 20:25
  • 1
    Set the option (under “My Project”) to “exit when the last window closes” (or whatever it is). – Ry- Oct 18 '14 at 20:29
  • @minitech I think this option is only available in VB.NET, in C# you would have to implement it yourself, if it's possible that easy. – Dominic B. Oct 18 '14 at 20:42

5 Answers5

4

You can start your NewForm in a new thread and create a new message loop

When the main message loop is closed, the application exits. In Windows Forms, this loop is closed when the Exit method is called

For more information see here.

var th = new Thread(() => Application.Run(new NewForm()));
th.SetApartmentState(ApartmentState.STA); // Deprecation Fix
th.Start();

this.Close();
offby1
  • 6,767
  • 30
  • 45
EZI
  • 15,209
  • 2
  • 27
  • 33
3

Another way to do it, is to manage the application context yourself. Here is a small demo:

 [STAThread]
 static void Main()
 {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     using (var myApplicationContext = new MyApplicationContext(new Form1()))
     {
        Application.Run(myApplicationContext);
     }
 }

You can define your tailored made ApplicationContext in the following way:

public class MyApplicationContext : ApplicationContext
{
    public MyApplicationContext(Form mainForm)
        :base(mainForm)
    {
    }

    protected override void OnMainFormClosed(object sender, EventArgs e)
    {
        if (Form.ActiveForm != null)
        {
           this.MainForm = Form.ActiveForm;
        }
        else
        {
           base.OnMainFormClosed(sender, e);
        }
    }
}

And now, you could do the following on the Button.Click event handler:

var f = new Form();
f.Show();
this.Close();

And the application will keep on running. Basically this way you keep the app alive while there is at least one active form.

NOTE Haven't tested it but it should work.

InBetween
  • 32,319
  • 3
  • 50
  • 90
1

Closing the form which the Program start in its main function will close the application, an idea is to have a parent Form and make it the main form, and never close it, this can be a hidden form if you want.

I am not working with Windows forms since long time ago but found on this page the reason behind behavior you are getting:
http://msdn.microsoft.com/en-us/library/ms157902(v=vs.110).aspx

Typically, the main function of an application calls this method and passes to it the main window of the application. This method adds an event handler to the mainForm parameter for the Closed event. The event handler calls ExitThread to clean up the application.

Also on this question How do I prevent the app from terminating when I close the startup form? there was a discussion about something the same

Community
  • 1
  • 1
Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301
1

I am not sure if it would work for you. But when I had the same problem while dealing with a login form... i just used ShowDialog() instead of Show() , (and for me it solved the problem ) Just Like:

        this.Hide();
        MainForm MForm = new MainForm();
        MForm.ShowDialog();
        this.Close();
HN Learner
  • 544
  • 7
  • 19
0

By default, a C# Forms application creates a "root" form in the Program.Main() method and passes that to the Application.Run() method. When this Form is closed, your program will exit.

However, you can change this behavior by using a different Application.Run() overload. Just don't pass the Form instance to Run(). Instead, show the form before calling Application.Run(), and then later on (when you finally do want the program to quit) use the Application.ExitThread() method to tell the Application class you're ready to close the application.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136