内存使用和导航的自定义控件问题

发布于 2024-10-14 23:37:17 字数 384 浏览 3 评论 0原文

自定义列表样式控件

上面是我正在处理的自定义控件的图像。它实际上是窗口窗体中的三个独立的控件。它是一个容器控件,包含“平台/控制台”控件的多个实例。 “平台”控件包含“游戏”控件的多个实例。控制台是可折叠的,这将隐藏列表中的游戏。

现在我遇到的问题是,当一个人玩了3000个游戏时,内存使用量会上升到500MB左右,并且控制非常迟缓。现在我还没有优化应用程序线程的创建,但即使在创建之后它也是一头猪。

人们将如何释放已移出屏幕的控件的资源?有没有比测试每个控件以查看它是否位于某个可见区域更快的方法?我应该有不同的方式来设计这个控件吗?

我对 C# 没有广泛的了解,因此任何建议将不胜感激。

Custom list style control

Above is an image for a custom control that I'm working on. It's actually three separate controls in a windows form. It's a container control which holds multiple instances of a "Platform/console" control. The "Platform" control holds multiple instances of a "Game" control. The consoles are collapsible which will hide the games in the list.

Now the problem I'm having is the fact that when a person has say 3000 game titles, the memory usage is up around 500MB, and the control is very sluggish. Now I haven't optimized the creation out of the application thread yet, but even after creation it's a pig.

How would a person go about freeing resources on controls that have moved off screen? Is there a quicker way than testing each control to see if it's in some visible area? Is there a different way I should be designing this control?

I don't have extensive knowledge in C# so any advice would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话难免假 2024-10-21 23:37:17

如果您使控件“虚拟化”,您可能会减少内存占用。看一下 ListView,如果设置ownerDrawn = true 和 VirtualMode =true 且 VirtualListSize =3000,以下代码将创建项目(并存储它们)并进行自定义绘图。

您可能需要一些额外的编程,因为在您的所有者绘制的控件上没有任何东西是免费的......

// should be a cache of some sort, WeakReference'd etc, this is NOT reducing memory load (it is adding memory load)
        Dictionary<Int32, ListViewItem> dict = new Dictionary<int, ListViewItem>();

        private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
        {

            ListViewItem lvi = null;
            if (dict.ContainsKey(e.ItemIndex))
            {

                Debug.WriteLine(String.Format("from cache:{0}", e.ItemIndex));
                lvi = dict[e.ItemIndex];
            }
            else
            {
                Debug.WriteLine(String.Format("created:{0}", e.ItemIndex));
                lvi = new ListViewItem { Text = String.Format("item:{0}", e.ItemIndex) };
                lvi.SubItems.Add( new ListViewItem.ListViewSubItem{Text = String.Format("si:{0}", e.ItemIndex)});
                dict.Add(e.ItemIndex, lvi);
            }
            e.Item = lvi; 
        }

        private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            // you can draw yourself...
            // e.Graphics.DrawString(e.Item.Text, System.Drawing.SystemFonts.DefaultFont, new SolidBrush(Color.Red), 0f, 0f);
            e.DrawText();
        }

If you make your Control "Virtualized" you might reduce the memory footprint. Have a look at ListView, if you set ownerDrawn = true and VirtualMode =true and VirtualListSize =3000 the following code will create items (and store them) and do custom drawing.

You might be in for some extra programming because on your owner drawn control nothing comes for free...

// should be a cache of some sort, WeakReference'd etc, this is NOT reducing memory load (it is adding memory load)
        Dictionary<Int32, ListViewItem> dict = new Dictionary<int, ListViewItem>();

        private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
        {

            ListViewItem lvi = null;
            if (dict.ContainsKey(e.ItemIndex))
            {

                Debug.WriteLine(String.Format("from cache:{0}", e.ItemIndex));
                lvi = dict[e.ItemIndex];
            }
            else
            {
                Debug.WriteLine(String.Format("created:{0}", e.ItemIndex));
                lvi = new ListViewItem { Text = String.Format("item:{0}", e.ItemIndex) };
                lvi.SubItems.Add( new ListViewItem.ListViewSubItem{Text = String.Format("si:{0}", e.ItemIndex)});
                dict.Add(e.ItemIndex, lvi);
            }
            e.Item = lvi; 
        }

        private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            // you can draw yourself...
            // e.Graphics.DrawString(e.Item.Text, System.Drawing.SystemFonts.DefaultFont, new SolidBrush(Color.Red), 0f, 0f);
            e.DrawText();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文