如何扩展一个WinForm的Dispose方法? [英] How do I extend a WinForm's Dispose method?

查看:450
本文介绍了如何扩展一个WinForm的Dispose方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到这样的警告从FxCop的:

I am getting this warning from FxCop:

'RestartForm包含字段RestartForm.done这是IDisposable的类型:'ManualResetEvent的关于修改RestartFormDispose方法调用Dispose或本领域关闭

"'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."

好吧,我明白这是什么意思,为什么这是需要做的事情......除了 System.Windows.Forms.Form中什么不允许您覆盖无论是 .Close() .Dispose(),所以做什么?目前我使用这一解决方案运行:

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:

    private void RestartForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        done.Set();
        done.Close();
    }

其工作方式是为我的应用程序...但FxCop的仍然显示此消息。我是覆盖,我可以放心地忽略它,或有另一种方式我应该这样做呢?

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?

推荐答案

您需要覆盖从表格的Dispose 方法C>

You need to override the Dispose method from Form

这通常在RestartForm.Designer.cs文件会被自动覆盖,所以你需要移动处理到code文件,这样就可以c您需要将没有它被改写添加添加任何$ C $由设计师。

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.cs

In the RestartForm.cs

protected override void Dispose(bool disposing)
{
  if (disposing)
  {
    if (components != null)
    {
      components.Dispose();
    }

    // Dispose stuff here
  }

  base.Dispose(disposing);
}

这篇关于如何扩展一个WinForm的Dispose方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆