Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to know when application is closing

Tags:

c#

resources

dll

Is there a way to monitor when an application that is using the dll closes? The DLL and the application are both C#, neither are Windows Forms.

I can check when the main application is closing, but I would like for the DLL to see "hey the program is closing and releasing me, I should do this real quick before I die".

Any way to do it? Or am I stuck having the application dish out "do this before you die"?

like image 500
Nick Avatar asked Jun 10 '10 06:06

Nick


2 Answers

Perhaps the AppDomain.ProcessExit event would work for you? Note the following though (from the docs):

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

like image 148
Fredrik Mörk Avatar answered Oct 26 '22 17:10

Fredrik Mörk


We don't know the details of your code but the fact that your class library needs to be aware of when the process is exiting might indicate a design flaw of your application.

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

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

  • MSDN: Implementing a Dispose Method

  • IDisposable: What Your Mother Never Told You About Resource Deallocation

For example you could have the following class in your class library:

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 your main module you can then use in the following way; the using statement will guarantee 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());
        }
    }
}
like image 32
Dirk Vollmar Avatar answered Oct 26 '22 17:10

Dirk Vollmar