就本例而言,我发现只须添加一个窗体,将它删除,然后强制执行垃圾收集,结果就会导致与被删除窗体相关联的许多类实例仍然处于活动状态。
For this example, I found that simply adding a form, deleting the form, and then forcing a garbage collection resulted in many class instances associated with the deleted form to still be alive.
用另外一个更合适的控件来完全替换当前控件可不容易,你需要删除这个控件,然后将一个新控件拖入窗体并重新配置它。
Completely replacing one control with another more relevant control is not as simple; you will need to delete an existing control, drop a new one on the form, and reconfigure it.
当作了这些修改以后,我使用调试器证实:当执行相同的测试案例时,与被删除的窗体相关联的对象计数减小。
After I made these code changes, I used the debugger to verify that the object count associated with the deleted form decreased when the same test case was executed.
随后,我添加了四个窗体,然后又将它们删除,并再次调用了垃圾收集器。
Next, I added, then deleted four forms and again invoked the garbage collector.
因此,这个静态hashtable(在应用程序的生存期内一直存在)永远不会删除引用每个窗体的那些键。
Therefore, this static hashtable, which essentially existed for the life of the application itself, was never removing the keys that referenced each form.
对于允许进行不可逆操作(如删除)的窗体,最好将“取消”按钮设置为默认命令按钮。
For a form that allows irreversible operations, such as deletions, it's a good idea to make the Cancel button the default command button.
对于允许进行不可逆操作(如删除)的窗体,最好将“取消”按钮设置为默认命令按钮。
For a form that allows irreversible operations, such as deletions, it's a good idea to make the Cancel button the default command button.
应用推荐