472,988 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 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 2062
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: celeong | last post by:
Hi, anybody can help me with this. I've created a singleton class, and now wants to add destructor to it. I know we can implement the IDisposable and also overrides the Finalize method (from...
2
by: mark | last post by:
I am developing an application in .Net C# that needs to restore a number of tool windows to some previous location and size. The problem I have is that when I create the form and set the Location...
4
by: Joe Abou Jaoude | last post by:
I m preparing to pass the 70-306 exam, so i downloaded Q & A from multiple sites. There's this question that really confuses me, coz i see that both answers A and C are both correct. Can anyone...
20
by: Charles Law | last post by:
I have an application that creates a class. The class has unmanaged resources, so must end gracefully. How can I guarantee that the unmanaged resources are freed? I have looked at IDisposable,...
2
by: Laurence Nuttall | last post by:
I have a .net solution that is a class library. I have a form in it called frmAbout I have a public sub, or I assume what will be a method to show the form. but when I type the frmabout name...
12
by: Joe Abou Jaoude | last post by:
hi, I have a component that uses a database connection. In the finalizer I dispose the connection because I read in msdn the following: "A type must implement Finalize when it uses...
5
dmjpro
by: dmjpro | last post by:
i know that the finalize method is called by JVM when an object is garbage collected. right??? but the ultimate memory release done by native OS. now my confusion is ... when it is called in...
19
by: rbrowning1958 | last post by:
Hello, I am confused by dispose etc. and hope someone can set me right. 1. The Dispose(Bool) the IDE generates for a form has nothing to do with IDisposable, right? 2. So when is this called?...
8
by: Rob | last post by:
This is a weird one... I've got a class called PageInfo that has the following finalize code: Protected Overrides Sub Finalize() MyBase.Finalize() Do While m_TempFolders.Count Dim TempPath As...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.