You need to know when the application closes.

Is there a way to track when an application using the dll closes? The DLL and application are both C # and Windows Forms.

I can check if the main application is closing, but I would like the DLL to see "hey that the program is closing and letting me go, I have to do this very quickly before I die."

How to do it? Or am I stuck when I posted the “do it before you die” application?

+6
c # dll resources
source share
4 answers

Perhaps the AppDomain.ProcessExit event will work for you? Pay attention to the following (from the documents):

The total execution time of all ProcessExit event handlers is limited, just as the total execution time of all finalizers is limited during the process failure. The default value is two seconds. An unmanaged host can change this runtime by calling the ICLRPolicyManager :: SetTimeout Method with an OPR_ProcessExit enumeration value.

+8
source share

We do not know the details of your code, but the fact that your class library should be aware of the process output may indicate a design flaw in your application.

If you need to free resources or clean up other things in a deterministic way, you should take a look at the IDisposable interface. If the classes represented by your library implement this interface, the caller can easily state that they no longer need the dll functionality by calling Dispose() .

Perhaps the following articles are a good starting point for further reading:

For example, your class library might have the following class:

 using System; using System.IO; public class ResourceManager : IDisposable { private Stream _resource; private bool _disposed; public void Dispose() { Dispose(true); // Use SupressFinalize in case a subclass // of this type implements a finalizer. GC.SuppressFinalize(this); } public void Dispose(bool disposing) { if (!_disposed) { if (disposing) { Console.WriteLine("Exiting Process. Cleaning up."); // free resources here if (_resource != null) _resource.Dispose(); Console.WriteLine("Object disposed."); } // Indicate that the instance has been disposed. _resource = null; _disposed = true; } } } 

In the main module, you can use it as follows; The using statement ensures that the Dispose() method is called:

 using System; using System.Windows.Forms; static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { using (ResourceManager manager = new ResourceManager()) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } 
+1
source share

AppDomain.ProcessExit will work fine, but should not be protected if the process terminates with the task manager, for example. Therefore, this may not be ideal if your device driver needs to release large resources, etc. (T.E. They cannot be released until the GC comes close to it).

See the Rick Stales Blog for more details.

0
source share

If this is a console application, you need to do as an accepted answer in this question . And it's best to use the IDisposable interface.

0
source share

All Articles