I have a polling thread which calls the following function and then sleeps for 20 seconds or so. I have been trying to determine why my application (a C# form) accumulates memory requirements up to 500Kbs over the duration of the application. When I remove the polling thread it appears to have a small constant memory usage over the duration of the application. I have concluded that the accessControl method within the thread is not freeing resources appropriately when it leaves the scope. Anyone familiar with this?
// 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;
}
}
}
// **********************************************************************************************************************************************************************