0

2 ペインの C# アプリケーションがあり、左側にツリー、右側にペインがあります。ツリー ノードをクリックすると、カスタム UserControl が右側に表示されます。このアプリには、クリックされたツリー ノードに応じて複数の UC があります。ユーザーが別のツリー ノードをクリックしたときにメモリ管理を行っていないため、必要な UC がそこにあった古いものに上書きされてしまいます。C# がメモリ管理を処理していると思っていましたが、ツリー ノードの 1 つをクリックし続けて別の場所をクリックし、同じツリー ノードをもう一度クリックすると、UC が読み込まれるたびにメモリ使用量が増え続けることがわかりました。クリックしてもメモリは解放されません。ここには管理されていないコードはありませんが、UC 上の Oracle でデータベース アクセスが発生し、DataGridView にレコードが読み込まれます。このツリー ノードをクリックするたびにメモリが上がらないようにするにはどうすればよいですか? Process Explorer を使用してメモリ使用量を監視しています。クリックをやめる前に、最大1 GBを超えました。右側のペインをロードするコードは次のとおりです。ありがとう。

private void LoadRightPane(TreeNode node)
    {
        splitPanelLeftRight.Panel2.Controls.Clear();

        try
        {
            this.Cursor = Cursors.WaitCursor;

            // Need to create user control on GUI thread
            // But we still will load the various grids on the BW thread
            UserControl uc = null;
            IUap uap;
            INodeTag nodeTag = (INodeTag)node.Tag;
            bool result;
            result = _uapList.TryGetValue(nodeTag.AssemblyTypeName, out uap);
            if (result)
            {
                uc = uap.Create(node); // decides which user control to "new" (based on node info) and returns it
            }

            if (uc != null)
                _bw.RunWorkerAsync(uc);
            else
            {
                this.Cursor = Cursors.Default;
            }
        }
        catch (Exception ex)
        {
            this.Cursor = Cursors.Default;
        }
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        if (!(e.Argument is IUapUserControl))
            throw new Exception("Exception: All user controls must implement the IUapUserControl interface.");
        IUapUserControl uc = e.Argument as IUapUserControl;
        uc.Populate(); // populates any info on UC
        e.Result = uc;
    }

    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        try
        {
            // check for an exception first
            if (e.Error != null)
            {
                return;
            }

            if (e.Result != null)
            {
                splitPanelLeftRight.Panel2.Controls.Add((UserControl)e.Result);
            }
        }
        catch (Exception ex)
        {
            LoggingWrapper.Logger.LogException(ex);
        }
        finally
        {
            this.Cursor = Cursors.Default;
        }
    }
4

0 に答える 0