c# - 不断添加和删除到 flowlayoutpanel winforms

标签 c# winforms flowlayoutpanel

我有一个 c# win 表单应用程序,其中有一个 flowLayoutPanel

我需要每秒更新这个面板中的所有 child 。

这是我当前的代码,每 1 秒在系统计时器中调用一次:

   public void RefreshReceiversPage()
    {
        if (checkBox_enableReceivers.Checked)
        {
            var receivers = OutgoingReceiverManager.GetCopyOfActiveRecieverHolders();

            for (int i = 0; i < flowLayoutPanel_receivers.Controls.Count; i++)
            {
                var tmp = flowLayoutPanel_receivers.Controls[i];
                flowLayoutPanel_receivers.Controls[i].Dispose();
                tmp.Dispose();
            }
            flowLayoutPanel_receivers.Controls.Clear();

            foreach (var item in receivers.ToList())
            {
                var tmpUc = new ucReceiverItem(item);
                if (flowLayoutPanel_receivers != null)
                {
                    try
                    {
                        flowLayoutPanel_receivers.Controls.Add(tmpUc);
                    }
                    catch
                    {
                    }
                }
            }
            receivers = null;
        }            
    }

现在这段代码完美运行了大约 2 分钟,然后突然间我开始收到 error creating window handle 因此我在上面的代码中 try catch 的原因。

但发生这种情况后, Pane View 变得很奇怪,我无法在不重新启动程序的情况下恢复它。

我到处搜索,似乎找不到有关抛出的异常的任何信息?

我能想到的是,我可能没有正确处理对象,并且它在某个地方耗尽了内存?

有没有人有任何建议或解决方案?

编辑:

这里是 UCRecieverItem:

public partial class ucReceiverItem : UserControl
{
    public ucReceiverItem(ReceiverHolder item)
    {
        InitializeComponent();
        ConstructItem(item);
        item = null;
    }

    private void ConstructItem(ReceiverHolder item)
    {
        label_name.Text = item.ReceiverDb.Name;
        label_numberOfConnections.Text = item.ReceiverOutgoingConnectionManager.GetNumberOfConnections().ToString();
        label_mrFilters.Text = item.ReceiverDb.MrFilters;
        label_multipleConnections.Text = item.ReceiverDb.MultipleConnections.ToString();
        //
        int count = item.GetActiveBufferCount();
        int size = item.GetActiveBufferSize();
        //
        label_bufferCount.Text = count + @" / " + size;
        progressBar_buffer.Maximum = size;
        progressBar_buffer.Minimum = 0;
        progressBar_buffer.Value = count;
    }
}

最佳答案

这段代码有问题:

for (int i = 0; i < flowLayoutPanel_receivers.Controls.Count; i++)
{
  var tmp = flowLayoutPanel_receivers.Controls[i];
  flowLayoutPanel_receivers.Controls[i].Dispose();
  tmp.Dispose();
}
flowLayoutPanel_receivers.Controls.Clear();

它仅处理容器中的一半 控件。另一半被 Controls.Clear(); 调用删除,但这些控件没有被释放——所以它们仍然存在并且正在耗尽内存。

每秒钟都这样做会使问题复杂化:这可能需要很多控制。

立即的解决方法是妥善处理控件:

while (flowLayoutPanel_receivers.Controls.Count > 0) {
  flowLayoutPanel_receivers.Controls[0].Dispose();
}

在那之后,我会质疑是否有必要每秒都这样做——这对用户来说似乎是一个恶劣的工作环境。

关于c# - 不断添加和删除到 flowlayoutpanel winforms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27041427/

相关文章:

c# - 在 FlowLayoutPanel 中拖放多个项目

c# - 我通常不检查函数输入是否无效。我错了吗?

c# - 如何使两种方式将 numericUpDown 绑定(bind)到成员类

c# - 如何在 Windows 窗体应用程序中随应用程序一起发布配置文件

c# - 表单获得焦点时 ObjectListView 上的黑色背景和伪像

c# - 在 flowLayoutPanel 中重新排列 CustomControl

c# - 任务完成时运行函数

c# - 将属性设置为虚拟时停止加载 Entity Framework

c# - 为什么这个 IndexOf 调用返回 -1?

c# - 如何从 flowlayoutpanel 更改控件的属性?