We do have console application:
namespace MyApplication
{
public void Main()
{
try
{
AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => Stop();
Start();
Console.ReadLine();
}
finally
{
Close();
}
}
public void Start()
{
//...
}
public void Stop()
{
//...
}
}
So if application is stopped proper (by pressing Enter in console window) Close() method is called from finally & AppDomain.CurrentDomain.ProcessExit
But when application is closed by Close button or terminated from Task Manager Close() method is not called at all.
Questions are:
- Why
finallyblock doesn't work? - How to handle console closing by
Close button?
UPD Even object's destructor is not called:
public class Watcher
{
Action _start;
Action _stop;
public Watcher(Action start, Action stop)
{
_start = start;
_stop = stop;
}
~Watcher()
{
_stop();
}
public void Start()
{
_start();
}
}
// And using as:
var w = new Watcher(Start, Stop);
w.Start();
Destructor is not called!