7

My understanding about this class is that you should use it when you want to be sure that the Finalizer (destructor) of the class is called, but from a couple of tests I did, it doesn't seem to be true. If it does not make sure that the dispose method is called, is there any other way of doing it? For example, if I want to make sure that some code is run to end my object, even if I close my program via Task Manager or something?

Pang
  • 9,564
  • 146
  • 81
  • 122
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
  • 1
    If some code could be run to end your object, then the Task Manager might be unable to close your program. This is why the Task manager 'End Process' stops your process without its cooperation by taking all execution away from it, so preventing the running of any code, cleanup or otherwise. – Martin James Apr 01 '12 at 08:43
  • I would only note that the question should be edited, or that it has identified a problem in the OPs understanding of disposal vs. finalization, as the Finalize() method and Dispose() method (and paradigms) are completely different. – Shaun Wilson Jan 20 '15 at 19:53

4 Answers4

8

Chris Brumme has explained this topic in a way I'm not sure could ever be topped. :)

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • Hmm, so seems CriticalFinalizerObject is not what I was looking at. Is there any way I can be certain that my Finalize method will be called, even if there's a ctrl+alt+del? – devoured elysium Jul 21 '09 at 03:23
  • If you are looking for reliable operations even if the OS crashes (power outage?), then you should look into transactional processing. – Sam Harwell Jul 21 '09 at 03:26
  • I don't need that much, I only need to be able to run some code when the application is closed or ctrl+alt+del'ed. – devoured elysium Jul 21 '09 at 03:32
  • 6
    "End Process" ends the process. Whatever you wanted to run, it's too late. – Sam Harwell Jul 21 '09 at 03:38
  • 1
    is this still true for .net 4.0 ? – Basit Anwer Jul 14 '11 at 12:28
  • Could someone edit the question to either update the link or post the full linked topic, please (I was not able to find the topic, so I can not update it myself)? Otherwise the answer is of no use. – qqqqqqq Feb 14 '20 at 18:06
4

from a couple of tests I did, it doesn't seem to be true.

Finalizers in .Net are non-deterministic. That means there's no guarantee exactly when the finalizer will be called. Just because an object went out of scope or even was disposed, doesn't mean the finalizer will be called right away. The garbage collector will get around to it at some unknown time in the future.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
3

If you really need code to run when when your program is Ctrl+Alt+Del'd, I don't think there's any other way than to have a separate program that monitors the first's state. If you really need that much architecture, I think you'd want to be using a service and some client apps, or a pair or services.

This is assuming, though, that you've already looked into the Application events. If you haven't, check out this overview.

EDIT Better than that overview, probably, is the ApplicationExit event.

overslacked
  • 4,127
  • 24
  • 28
1

The Finalize() method and Dispose() method are different things.

By default, Dispose would never be called. You would have to call it yourself from the Finalize method. Consider the following (ignoring the obvious failures in a proper finalize/dispose pattern here, for brevity):

public class Foo : IDisposable
{
    public void Dispose() 
    {
        // NOP
    }

    ~Foo()
    {
        Dispose();
    } 
}
Shaun Wilson
  • 8,727
  • 3
  • 50
  • 48