Why does my destructor never run?(为什么我的析构函数从不运行?)
问题描述
我有一个带有析构方法的空白 Winform
public 部分类 Form1 : Form{公共表格1(){System.Diagnostics.Trace.WriteLine("Form1.Initialize " + this.GetHashCode().ToString());初始化组件();}~Form1(){System.Diagnostics.Trace.WriteLine("Form1.Dispose" + this.GetHashCode().ToString());}}
当表单被销毁时,我希望它写入输出窗口:
<上一页>(Form1打开)Form1.初始化 41149443(Form1 关闭)Form1.Dispose 41149443
MSDN 提出了 3 种实现析构函数的方法:
~析构函数()http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx
一次性http://msdn.microsoft.com/en-us/library/system.idisposable.aspx
SafeHandle 模式http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle.aspx
但是,这些方法都没有将Form1.Dispose 41149443"写入输出窗口.因此,我无法判断表格是否已被销毁.建议?
由于垃圾收集器的不确定性,我是否应该放弃实现这一目标的希望?
有没有其他方法可以知道 Form1 是否被垃圾回收了?
您列出的实现析构函数的三种方法中只有一种实际上涉及析构函数,那就是 ~Destructor()
.
如果您实现 IDisposable
并处置您的对象,那么 Dispose
中的代码将运行,但没有理由认为您的析构函数会运行.
我认为你在这里追逐不可能的事情.析构函数按照垃圾收集器的命令运行.这不是你可以控制的东西.GC 完全有权形成运行析构函数只是浪费时间的观点,如果有足够的内存,它就会形成这种观点.
如果您需要可预测的处置、最终确定等,请使用 IDisposable
.
I have a blank Winform with a destructor method
public partial class Form1 : Form
{
public Form1()
{
System.Diagnostics.Trace.WriteLine("Form1.Initialize " + this.GetHashCode().ToString());
InitializeComponent();
}
~Form1()
{
System.Diagnostics.Trace.WriteLine("Form1.Dispose " + this.GetHashCode().ToString());
}
}
When the form is destroyed, I want it to write to the output window:
(Form1 opened) Form1.Initialize 41149443 (Form1 closed) Form1.Dispose 41149443
MSDN suggests 3 ways in implementing destructor:
~Destructor() http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx
IDisposable http://msdn.microsoft.com/en-us/library/system.idisposable.aspx
SafeHandle pattern http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle.aspx
However, none of these ways write "Form1.Dispose 41149443" to the output Window.
Therefore, I am not able to tell whether the form has been destroyed or not. Suggestions ?
Should I give up hope on achieving this due to uncertainty of garbage collector?
Is there another way to know whether Form1 has been garbage collected ?
Only one of the three ways to implement a destructor that you list actually involves a destructor, and that's ~Destructor()
.
If you implement IDisposable
, and dispose of your object, then the code in Dispose
will run, but there's no reason to think that your destructor will.
I think you chasing the impossible here. Destructors run as and when the garbage collector so decrees. It's not something that you have any control over. The GC is well within its rights to form the opinion that running destructors simply wastes time, and if there is plenty of memory it will form that opinion.
If you need predictable disposal, finalization etc., then use IDisposable
.
这篇关于为什么我的析构函数从不运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我的析构函数从不运行?
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01