I developed a class that compiles to a dll and my friend uses this dll and creates an instance of my class.
I want to do some action with my local variable when the user turns my friend’s program (GUI) off.
I tried destructor but it’s not deterministic.
My friend’s program doesn’t contain using and cannot compile this program again therefore I can’t use dispose method.
Any idea how to do it in another way?
thanks
I can’t use dispose like this :
using System;
using System.Text;
class Program
{
static void Main()
{
// Use using statement with class that implements Dispose.
using (SystemResource resource = new SystemResource())
{
Console.WriteLine(1);
}
Console.WriteLine(2);
}
}
class SystemResource : IDisposable
{
public void Dispose()
{
// The implementation of this method not described here.
// ... For now, just report the call.
Console.WriteLine(0);
}
}
Jon is correct, however, you could attempt a dirty work-around…
You could wrap your friends application with another that, within the main method, calls his main method, then locate the main window (e.g. if it is a WPF application using Application.MainWindow) then sign up to that windows Closed event.
You would still need to get the instance of your class. You could do this by adding a static list of instances to your class.
This workaround is not something I’d highly recommend, but it could work.
(Not recommended because it assumes access to your class ends as window closes.)
If possible a bit better approach would to use the applications events and not the windows (WPF event and Winforms event).
If the host application (that hosts your class) is really your friends, you could ask him to either change it, or, if he lost the code, for permission to recreate the code (using Reflector or a similar tool) and modify it.