我有一个轮询线程,它调用以下函数,然后休眠 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;
}
}
}
// **********************************************************************************************************************************************************************