Finalizers are typically implemented to ensure resources that are beyond the garbage collector’s domain of responsibility get cleaned up properly. A good example is when you must deal with unmanaged resources (for example, in Win32 interop scenarios requiring manual release of object using calls to
CloseHandle). Although lots of Win32 APIs have been wrapped in the .NET Framework, you might sometimes need to P/Invoke some other APIs manually. As an example, consider the
CreateJobObject Win32 function, which allows for the creation of jobs, which are groups of processes. A managed code wrapper could look like this:
Don’t worry too much about the P/Invoke signatures, which are a subject on their own. Adam ...