Finalization order and using finalizers to commit changes

F

Fernando Cacciola

Hi,

In the context of C#:

Comming from C++ and new to C# it just ocurred to me to put some code in
the finalizer of a class as a mean to commit changes whatever happens.
It would have worked right if it were not for the fact that when I close the
application this finalizer is called after GDI+ uninitializes,
and so, for instance, creating a new Matrix() object raises a "GDI+ is not
properly initialized".

I think I'll forget about using the finalizer for this, but anyway, I'm
curious: is there any way to have some control of the order of finalization,
or at least to tell whether some resource, like GDI+, was finalized already?

TIA

Fernando Cacciola
SciSoft
 
M

Mattias Sjögren

Fernando,
I think I'll forget about using the finalizer for this, but anyway, I'm
curious: is there any way to have some control of the order of finalization,

No. But if you haven't done so already, read about the Dispose pattern
in the docs.

or at least to tell whether some resource, like GDI+, was finalized already?

Well you can check Environment.HasShutdownStarted to see if the
appdomain is shutting down.



Mattias
 
C

Chris Lyon [MSFT]

Mattias is correct. You have no control over the order of finalization. That means you should not rely on *anything* still being alive while your finalizer's code is being run.

See Chris Brumme's post on finalization for more information:
http://blogs.msdn.com/cbrumme/archive/2004/02/20/77460.aspx

-Chris
Fernando,


No. But if you haven't done so already, read about the Dispose pattern
in the docs.



Well you can check Environment.HasShutdownStarted to see if the
appdomain is shutting down.



Mattias


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top