简体   繁体   English

处置TabPage控件及其控件

[英]Disposing a TabPage control and its controls

My application uses TabControl to show data, each time the user asks for data the application will create a new TabPage which contains many controls ( PictureBox controls, Label controls, .etc). 我的应用程序使用TabControl来显示数据,每当用户要求数据时,应用程序将创建一个包含许多控件( PictureBox控件, Label控件等)的新TabPage The user at some point will close the TabPage controls he is done working with. 用户将在某个时候关闭完成工作的TabPage控件。 Now the problem here is the closed TabPage controls, they will pile up in the memory causing the program to use a huge part of the memory. 现在这里的问题是已关闭的TabPage控件,它们将堆积在内存中,导致程序占用内存的很大一部分。 Removing the TabPage controls from the TabControl does not dispose it nor dispose the items within, it just hides it but the control still hangs in the memory. TabControl中删除TabPage控件不会对其进行处置,也不会对其进行处置,它只是将其隐藏,但该控件仍挂在内存中。

The code I am using to remove the TabPage is something like: 我用来删除TabPage的代码是这样的:

tabControl.TabPages.Remove(myTabPage);
myTabPage.Dispose();   

Q: How do I totally get rid of the TabPage controls? 问:如何完全摆脱TabPage控件?

您可以每次都通过与其关联的选项卡控件进行迭代,并将其循环放置。

            tabControl1.TabPages[0].Controls[i].Dispose();

Calling myTabPage.Dispose() should dispose the resource of the TabPage and its children. 调用myTabPage.Dispose()应该处理TabPage及其子级的资源。 However, the memory used by these objects might not be necessarily released right away by GC, even if these objects are not referenced by the others. 但是,即使这些对象没有被其他对象引用,GC也不一定会立即释放这些对象使用的内存。

In a computer with very large memory, GC tends to be lazy. 在内存很大的计算机中,GC往往很懒。 As long as the memory will be released later, say, minutes later, you don't need to worry. 只要稍后再释放内存,例如几分钟后,您就不必担心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM