C# 如何扩展 WinForm 的 Dispose 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1052147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I extend a WinForm's Dispose method?
提问by Matthew Scharley
I am getting this warning from FxCop:
我从 FxCop 收到此警告:
"'RestartForm' contains field 'RestartForm.done' that is of IDisposable type: 'ManualResetEvent'. Change the Dispose method on 'RestartForm' to call Dispose or Close on this field."
“'RestartForm' 包含 IDisposable 类型的字段 'RestartForm.done':'ManualResetEvent'。更改 'RestartForm' 上的 Dispose 方法以在该字段上调用 Dispose 或 Close。”
Ok, I understand what this means and why this is what needs to be done... Except System.Windows.Forms.Form
doesn't allow you to override either .Close()
or .Dispose()
, so what to do? Currently I'm running with this solution:
好的,我明白这意味着什么以及为什么这是需要做的......除了System.Windows.Forms.Form
不允许你覆盖.Close()
or .Dispose()
,那该怎么办?目前我正在使用这个解决方案:
private void RestartForm_FormClosing(object sender, FormClosingEventArgs e)
{
done.Set();
done.Close();
}
Which works as intended for my application... But FxCop still shows this message. Am I covered and can I safely ignore it, or is there another way I should be doing this?
这适用于我的应用程序......但 FxCop 仍然显示此消息。我被覆盖了吗,我可以安全地忽略它,还是有另一种方式我应该这样做?
采纳答案by heavyd
You need to override the Dispose
method from Form
您需要Dispose
从Form
Typically this is automatically overridden in the RestartForm.Designer.cs file, so you will need to move the dispose into your code file so that you can add whatever code you need to add without it being rewritten by the designer.
通常,这会在 RestartForm.Designer.cs 文件中自动覆盖,因此您需要将处置移动到您的代码文件中,以便您可以添加任何需要添加的代码,而无需设计人员重写。
In the RestartForm.cs
在 RestartForm.cs 中
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
// Dispose stuff here
}
base.Dispose(disposing);
}
回答by Shay Erlichmen
You need to override the Dispose method, this method comes from the Controlbase class
需要重写Dispose方法,该方法来自Control基类
protected override void Dispose(bool disposing)
{
if (disposing)
{
event.Dispose();
}
base.Dispose(disposing);
}
回答by womp
If RestartForm extends System.Windows.Forms.Form, you should be able to override Dispose(bool disposing). You should properly implement this for your "RestartForm" class to dispose of your IDisposables.
如果 RestartForm 扩展 System.Windows.Forms.Form,您应该能够覆盖 Dispose(bool disposing)。您应该为您的“RestartForm”类正确实现它以处理您的 IDisposables。
It should look like:
它应该看起来像:
public override Dispose(bool disposing)
{
if (disposing)
{
// Dispose was called from user code. Dispose of managed resources here.
done.Dispose();
}
// Dispose of unmanaged resources here, and invoke base dispose.
base.Dispose(disposing);
}
回答by AlexTheo
I use this method :)
我用这个方法:)
Image bgImage = Image.FromFile(workingDir + "\" + button.BackgroundImage);
currentButton.Image = bgImage;
currentButton.Disposed += (Object sndr, EventArgs evnt) => bgImage.Dispose();