Finalizer is not called

I have a class in C # where I want to close some communication ports properly when my class is deleted. However, the finalizer is never called when I exit the program. Why is this? Am I doing something wrong?

I call the utility manually, which goes through and closes all messages. This does not work.

Here is the finalizer I'm using:

~Power()
{
    Dispose(false);
}
+3
source share
4 answers

The finalizer (which you use here) is called only during the completion phase, which will be executed during the GC.

If you implement IDisposable correctly, this should never be called. I will talk about this in detail in my series on IDisposable .

, " " , . , . IDisposable () , .

+7

# . ++.

, IDispose :

using (Power power = new Power())
{
    //  blah blah
}

, IDispose - Dispose, , ; , . MSDN - Dispose() . , , , .

+4

, , ? , :

using System;

class Program {
    static void Main(string[] args) {
        new Test();
    }
}

class Test {
    ~Test() { Console.Beep(); }
}

, , . , . Environment.FailFast(), .

+4

Finalizer , , . .

0

All Articles