471,356 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,356 software developers and data experts.

A Form's Finalize Method

Hello, Newsgroupians:

I've a question regarding a finalizer. Why is it that the finalize method
of a form is never being called?

I create a simple program...

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

~Form1()
{
Console.Beep();
// MessageBox.Show("Cleaning up!");
}
}

static class Program
{
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}

Why does it not beep or show a message box?

Thank you.
Trecius
Aug 4 '08 #1
9 2023
Just a guess, but IIRC, Application.Run disposes (i.e. calls
Dispose()) the Form instance afterwards, and the Dispose() method of
many types disables the finalizer (after all, it has been tidied
already!).

Marc
Aug 4 '08 #2
Trecius <Tr*****@discussions.microsoft.comwrote:
I've a question regarding a finalizer. Why is it that the finalize method
of a form is never being called?
<snip>
Why does it not beep or show a message box?
Application.Run calls Dispose on the form. Disposing the form
suppresses the finalizer.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 4 '08 #3
Yup; Form : Component, and Component has:

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

which probably explains it
Aug 4 '08 #4
Thank you, Mr. Skeet and Mr. Gravell. You're comments and answers are most
welcomed!

Trecius

"Jon Skeet [C# MVP]" wrote:
Trecius <Tr*****@discussions.microsoft.comwrote:
I've a question regarding a finalizer. Why is it that the finalize method
of a form is never being called?

<snip>
Why does it not beep or show a message box?

Application.Run calls Dispose on the form. Disposing the form
suppresses the finalizer.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 4 '08 #5
Mr. Skeet:

One more question related to my first. I'm trying to override the Dispose()
method for the form. However, when I try...

public override void Dispose()
{
...
}

I receive an error, stating it's not virtual. Of course it is not virtual,
so instead of the keyword 'override,' I use the keyword 'new.' However, I
still see that my Dispose() method is not being called. What can I do to
assure that my Dispose method is being called without having to dispose of it
manually after the Application.Run(frm)?
Thank you again.
Trecius

"Jon Skeet [C# MVP]" wrote:
Trecius <Tr*****@discussions.microsoft.comwrote:
I've a question regarding a finalizer. Why is it that the finalize method
of a form is never being called?

<snip>
Why does it not beep or show a message box?

Application.Run calls Dispose on the form. Disposing the form
suppresses the finalizer.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 4 '08 #6
I receive an error, stating it's not virtual.

There is a common pattern of having a virtual Dispose(bool disposing),
which both the finalizer and Dispose call, passing false/true
respectively. Try overriding this:

protected override void Dispose(bool disposing)
{
if (disposing)
{
// regular disposal
}
base.Dispose(disposing);
}

But note that the IDE may already have done this... ;-(
Aug 4 '08 #7
On Mon, 04 Aug 2008 09:17:00 -0700, Trecius
<Tr*****@discussions.microsoft.comwrote:
[...] What can I do to
assure that my Dispose method is being called without having to dispose
of it
manually after the Application.Run(frm)?
You should override the Dispose(bool) method instead.

Note that the VS Designer may have already provided this method. My
recollection is that you can safely edit the Designer.cs file _for that
method only_ without the Designer overwriting your changes. But if that's
not correct, I think if you move the method into the non-Designer .cs
file, that should work.

Pete
Aug 4 '08 #8
Nevermind. I forgot that the class is a partial class, so the Dispose()
method is already defined on it. I found the place where I need to add my
code. Thank you.
Trecius

"Jon Skeet [C# MVP]" wrote:
Trecius <Tr*****@discussions.microsoft.comwrote:
I've a question regarding a finalizer. Why is it that the finalize method
of a form is never being called?

<snip>
Why does it not beep or show a message box?

Application.Run calls Dispose on the form. Disposing the form
suppresses the finalizer.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 4 '08 #9
Trecius <Tr*****@discussions.microsoft.comwrote:
One more question related to my first. I'm trying to override the Dispose()
method for the form. However, when I try...

public override void Dispose()
{
...
}

I receive an error, stating it's not virtual. Of course it is not virtual,
so instead of the keyword 'override,' I use the keyword 'new.' However, I
still see that my Dispose() method is not being called. What can I do to
assure that my Dispose method is being called without having to dispose of it
manually after the Application.Run(frm)?
Assuming you're using the designer, the Dispose method is already
implemented in the designer-generated partial class definition.

Could you perhaps subscribe to the Control.Disposed event for your
form?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 4 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by celeong | last post: by
4 posts views Thread by Joe Abou Jaoude | last post: by
20 posts views Thread by Charles Law | last post: by
2 posts views Thread by Laurence Nuttall | last post: by
19 posts views Thread by rbrowning1958 | last post: by
8 posts views Thread by Rob | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.