Free com objects when stealing application

Is there a way to free com objects when the application crashes?

I have the following code:

public class Application : IDisposable { private bool disposed = false; private object realApplication; public void Dispose() { Dispose(true); } private void Dispose(bool disposing) { if (!disposed) { if (realApplication!=null) { Marshal.ReleaseComObject(realApplication); realApplication = null; } disposed = true; } GC.SuppressFinalize(this); } ... ~Application() { Dispose(false); } } 

But it frees the com object only when the regular application closes.

+4
source share
3 answers

Try making the Application class an inherited CriticalFinalizerObject .

+3
source

The console close button should not crash - look at the SetConsoleCtrlHandler function. This allows you to set a handler that is called when the user clicks the close button, which allows you to clear.

You cannot guarantee the possibility of cleaning under any circumstances; it’s easy to kill the application, preventing it from being cleaned. For example, your application will never be able to detect a user killing him through the task manager.

If it is important that the application is cleaned up under any circumstances, you can see if it has a second application that monitors the first one and cleans it if necessary.

+1
source

COM will create out-of-process server cleanup objects if the client crashes, but only objects that do not use any methods. All running methods executed on an object must be executed before this object can be released. There are two reliable solutions: either run all the methods in a very short period of time, or create a separate process for working with the COM server.

+1
source

Source: https://habr.com/ru/post/1314921/


All Articles