0

我有一个轮询线程,它调用以下函数,然后休眠 20 秒左右。我一直在尝试确定为什么我的应用程序(C# 表单)在应用程序的持续时间内累积了高达 500Kbs 的内存需求。当我删除轮询线程时,它似乎在应用程序的持续时间内有一个小的恒定内存使用量。我得出的结论是,线程中的 accessControl 方法在离开范围时没有适当地释放资源。有谁熟悉这个?

    // Method that accesses Form objects that must be accessed by original thread
    private void accessControl()
    {
        int secCount = 600;
        bool[] newViolation;
        bool tempBool = false;
        tempBool = label3.InvokeRequired || labelSecondLargest.InvokeRequired || labelThirdLargest.InvokeRequired;
        if (tempBool)
        {
            System.Console.WriteLine("CHANGING LABELS");
            labelLargest.Invoke(new MethodInvoker(delegate
            {
                newViolation = checkViolations();
                Draw(Assets);
                Thread.Sleep(secCount);
                int counter = 0;
                int duration = 3;
                while(counter < duration)
                {
                    if(newViolation[0])
                        labelLargest.ForeColor = System.Drawing.Color.Red;
                    if (newViolation[1])
                        labelSecondLargest.ForeColor = System.Drawing.Color.Red;
                    if (newViolation[2])
                        labelThirdLargest.ForeColor = System.Drawing.Color.Red;

                    System.Windows.Forms.Application.DoEvents();
                    Thread.Sleep(secCount);                         // Wait secCount/1000 seconds before moving on...
                    if (newViolation[0])
                        labelLargest.ForeColor = System.Drawing.Color.Blue;
                    if (newViolation[1])
                        labelSecondLargest.ForeColor = System.Drawing.Color.Green;
                    if (newViolation[2])
                        labelThirdLargest.ForeColor = System.Drawing.Color.Purple;
                    counter++;                                      // Do this 'counter' many times
                }
            }));
        }
        else
        {
           System.Console.WriteLine("CHANGING LABELS 2");
           newViolation = checkViolations();
           Draw(Assets);
           Thread.Sleep(secCount);
           int counter = 0;
           int duration = 3;
           while(counter < duration)
           {
                if(newViolation[0])
                    labelLargest.ForeColor = System.Drawing.Color.Red;
                if (newViolation[1])
                    labelSecondLargest.ForeColor = System.Drawing.Color.Red;
                if (newViolation[2])
                    labelThirdLargest.ForeColor = System.Drawing.Color.Red;

                System.Windows.Forms.Application.DoEvents();
                Thread.Sleep(secCount);                         // Wait secCount/1000 seconds before moving on...
                 if (newViolation[0])
                    labelLargest.ForeColor = System.Drawing.Color.Blue;
                 if (newViolation[1])
                    labelSecondLargest.ForeColor = System.Drawing.Color.Green;
                 if (newViolation[2])
                     labelThirdLargest.ForeColor = System.Drawing.Color.Purple;
                }
            }
        }
    // **********************************************************************************************************************************************************************
4

1 回答 1

0

正如有人提到的,Draw(Assets) 方法是问题的根源。该方法负责声明和实例化表示 winform 中不同选项卡的新 DataGridView 对象。

然而,这总是创建新的 DataGridView 对象而没有正确删除旧的对象(我想,因为这是一个表单属性,所以一旦超出 Draw() 的范围,正常的内存管理就不会应用)。

为了解决这个问题,我将这些变量设为全局变量并仅在不存在(null)时实例化它们。

于 2013-08-13T21:58:14.740 回答