在System.Windows.Form派生类中的哪里放置资源? [英] Where to dispose resources in a System.Windows.Form-derived class?

查看:74
本文介绍了在System.Windows.Form派生类中的哪里放置资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,该表单在其构造函数中创建了一些我需要处理的一次性资源.但是,C#表单设计器已经在Designer.cs文件中生成了一个Dispose()方法,该方法似乎没有任何类型的用户挂钩.因此,我对如何实现典型的IDisposable模式不知所措.

I've got a form that creates a couple of disposable resources in its constructor that I need to dispose of. However, the C# form designer already produces a Dispose() method in the Designer.cs file that doesn't appear to have any type of user hook in it. So I'm at a loss as to how I'm supposed to implement the typical IDisposable pattern.

此表单是偶尔创建的,但从未显示,因此使用Close事件将无济于事.

This form is occasionally created but never shown, so using the Close event won't help.

有问题的对象不是IComponents,所以我不能只是将它们添加到this.components.

The objects in question are not IComponents, so I can't just add them to this.components.

我可以在哪里放置此清理代码,并确保在处理表单后它可以运行?

Where can I put this clean-up code and be sure it will run when the form is disposed?

推荐答案

您可以将Dispose()方法从Designer.cs文件移动到源代码文件.并对其进行更改以添加对您的表单类中的所有一次性成员的处置请求.这是可以编辑设计器文件的少数情况之一,您只需要远离#region内标记为"Windows Form Designer生成的代码"的代码即可.

You can move the Dispose() method from the Designer.cs file to your source code file. And alter it to add dispose calls for any disposable members in your form class. This is one of the few cases where it is okay to edit the designer file, you only need to stay away from the code that inside the #region marked "Windows Form Designer generated code".

这篇关于在System.Windows.Form派生类中的哪里放置资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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