执行某些静态终结的正确方法是什么?
没有静态析构函数。 AppDomain.DomainUnload
事件不会在默认域中引发。 AppDomain.ProcessExit
事件在所有事件处理程序之间共享三秒的总时间(默认设置),因此它并不是真正可用的。
最佳答案
Herfried Wagner has written an excellent article explaining how to implement this – alas, in German (and VB). Still, the code should be understandable.
我试过了:
static readonly Finalizer finalizer = new Finalizer();
sealed class Finalizer {
~Finalizer() {
Thread.Sleep(1000);
Console.WriteLine("one");
Thread.Sleep(1000);
Console.WriteLine("two");
Thread.Sleep(1000);
Console.WriteLine("three");
Thread.Sleep(1000);
Console.WriteLine("four");
Thread.Sleep(1000);
Console.WriteLine("five");
}
}
它的工作方式似乎与 AppDomain.ProcessExit
事件完全相同:终结器获取 ca。三秒……
关于c# - 静态终结器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/256077/