Llo*_*ell 6 .net c# destructor
我想知道在这些情况下何时调用析构函数,如果它是在主UI线程上调用它?
假设我有以下代码,什么时候会调用析构函数,它会等到我完成所有函数调用之后?
private void Foo()
{
MyObject myObj = new MyObject();
DoSomeFunThingsWithMyObject(myObj);
myObj = new MyObject(); //is the destructor for the first instance called now?
DoLongOminousFunctionality(myObj);
}
//Or will it be called after the DoLongOminousFunctionality?
Run Code Online (Sandbox Code Playgroud)
如果线程在myObj = new MyObject()中断,或者Destructor调用等待直到Thread空闲,那么这只是我感兴趣的东西.
谢谢你的信息.
Bog*_*_Ch 17
当垃圾收集器决定必须清理一些旧对象时,将调用析构函数.您不能依赖.NET中的析构函数执行时间
如果你想在不需要时清理一些资源(特别是当你有任何非托管资源,如TCP连接,SQL连接等)时,你应该使用Dispose().
请参阅实现Dispose方法
| 归档时间: |
|
| 查看次数: |
3852 次 |
| 最近记录: |