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:
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());
}
}
}