Hot Cool Stud
Hot Cool Stud

Reputation: 1205

Implement using in c# without show dialogue win forms

I am developing a winforms application with the code below to open new form :

 using (Form1 f = new Form1(textBoxJobCardNo.Text, tf, tfh))
                    {
                       // System.GC.Collect();
                        f.FormBorderStyle = FormBorderStyle.None;
                        f.Left = sc[1].Bounds.Left;
                        f.Top = sc[1].Bounds.Top;
                        f.Height = sc[1].Bounds.Height;
                        f.Width = sc[1].Bounds.Width;
                        f.StartPosition = FormStartPosition.Manual;
                        labelerror.Visible = false;
                        textBoxJobCardNo.Clear();
                        f.ShowDialog();


                    }

I am using ShowDialog() to open new form. In place of ShowDialog() I want to use Show() without removing using statement because the using statement helps me to free memory after form close. If I use Show() then it will not hold on after Show() and moves out of using scope, which will close the form.

When i am using using my ram usage is constant when i am trying to do it with show without using it increase 2 mb on every form open.

Can't i hold control in using like show dialog with show.

Upvotes: 0

Views: 325

Answers (3)

Roland
Roland

Reputation: 5234

Form.Show() should be called without a using block because it will return immediately after showing the form, so that the user can access the new form and all other forms that are present. In a using block the newly shown form would be destroyed immediately after creation because of the automatic Dispose() call at the end of the block.

Form.ShowDialog() returns much later: after the Modal form is closed, so this will work fine in a using block.

Upvotes: 0

Jcl
Jcl

Reputation: 28272

If you don't show the form modally (using ShowDialog()), by default, it'll be disposed on closing. This is what the MSDN says:

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.

So there's no need to call Dispose() (or use a using block) if the form is not modal and not MDI, it'll be freed when closed.

That said, this is an implementation detail that might change (although at this point in time, it's unlikely that it will), and it should be safe to call Dispose() multiple times if you want.

Upvotes: 1

Florian Schmidinger
Florian Schmidinger

Reputation: 4692

Put this line of code in the FormClosed Eventhandler of the form

  this.Dispose();

aka:

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Dispose();
    }

The using block does nothing more than call dispose() on the resource you specify after the block ended. If you handle Disposing your Resources yourself you dont have to use the using block and your Resources are freed the way you specify, in this example after the Form has been closed.

Edit:

protected override void OnFormClosed(FormClosedEventArgs e)
    {
        base.OnFormClosed(e);
        this.Dispose();
    }

Upvotes: 0

Related Questions