private void CheckMemoryWhileWaitingForUser(object sender, EventArgs e) { //Called by timer to update currentMemoryUsageBar memoryUsageForGUI = SystemMemory.GetMemoryPercentUsed(); currentMemoryUsageBar.Foreground = SharedStatics.CalculateMemoryBrush(memoryUsageForGUI); currentMemoryUsageBar.ToolTip = String.Format("System memory at {0:F2}%", memoryUsageForGUI); }
private void SetSystemMemoryPercentAndLabel() { //Gets the system memory percent and performs GUI tasks. systemMemoryPercent = SystemMemory.GetMemoryPercentUsed(); systemMemoryLabel.Content = string.Format("{0:F2}", systemMemoryPercent); //Calcualte colors Brush systemBasedBrush = SharedStatics.CalculateMemoryBrush(systemMemoryPercent); //Apply brush wherever systemMemoryLabel.Background = systemBasedBrush; warningLabel.Background = systemBasedBrush; }
private void CheckMemoryAndCreateWarnings(object sender, EventArgs e) { //Checks if a warning window should me made. //Only one warning window is permitted at a time double memoryUsage = SystemMemory.GetMemoryPercentUsed(); //Check which warnings should be re-enabled, assume sorted ascending foreach (WarningEvent warning in warnings) { if (warning.enabled == false) { //Check if the memory went low enough to re-enable the warning if ((userSettings.warningResetThreshold < warning.threshold) && (memoryUsage <= (warning.threshold - userSettings.warningResetThreshold))) { warning.enabled = true; } } else { break; //Must have re-enabled all we can } } //Now find the index of the latest warning to activate // This index skips duplicate, old warnings, so the user // will only see one popup window at a time. int lastWarningIndexToActivate = -1; for (int n = 0; n < warnings.Count; ++n) { if (memoryUsage >= warnings[n].threshold) { if (warnings[n].enabled) { lastWarningIndexToActivate = n; } } else { break; //Will be no warnings to activate above this one } } if (lastWarningIndexToActivate != -1) { //First, disable all warnings that are going to be skipped over for (int n = 0; n < lastWarningIndexToActivate; ++n) { warnings[n].enabled = false; } //Terminate any warning window still open CloseAnOpenWarningWindow(); //Open the new warning window warnings[lastWarningIndexToActivate].enabled = false; warnings[lastWarningIndexToActivate].warningWindow = new WarningWindow( warnings[lastWarningIndexToActivate].threshold, warnings[lastWarningIndexToActivate].type, userSettings); warnings[lastWarningIndexToActivate].warningWindow.Show(); } }
private void KillMemoryHogs() { String tempOutput = ""; StreamWriter log = new StreamWriter("log.txt", true); log.AutoFlush = true; int attemptedKillCount = 0; while (systemMemoryPercent > memoryExceededThreshold) { tempOutput = String.Format("{0} System memory {1:F2}% > {2:F2}%\n", DateTime.Now.ToString(), systemMemoryPercent, memoryExceededThreshold); log.Write(tempOutput); Process hog = processesSorted[attemptedKillCount]; log.Write("Killing process:\n"); log.Write("\tName:\t" + hog.ProcessName + "\n"); log.Write("\tPID:\t" + hog.Id + "\n"); log.Write("Result:\t"); Idk processKilled = Idk.IDK; try { hog.Kill(); } catch { processKilled = CheckProcessIsKilled(hog); if (processKilled != Idk.TRUE) { //Maybe the process is killed, log.Write("Kill failed.\n\n"); continue;// but move on from this error anyways. } } finally { ++attemptedKillCount; } if (processKilled == Idk.IDK) { processKilled = CheckProcessIsKilled(hog); } //Documentation states Kill() is asynchronous, //and I do not want to block until the process is killed. //Kill() did not error, so wait a little to see if it worked int waitCount = 0; while ((processKilled != Idk.TRUE) && (waitCount < 10)) { System.Threading.Thread.Sleep(100);//0.1 seconds ++waitCount; processKilled = CheckProcessIsKilled(hog); } //If process was killed, wait 1 second before killing the next. if (processKilled == Idk.TRUE) { log.Write("Kill succeeded.\n\n"); System.Threading.Thread.Sleep(1000); } else if (processKilled == Idk.FALSE) { log.Write("Kill failed.\n\n"); } else { log.Write("Kill status uncertain.\n\n"); } //Checking RAM again, whether the killed process was exited or not systemMemoryPercent = SystemMemory.GetMemoryPercentUsed(); } tempOutput = String.Format("{0} System memory {1:F2}% is below the warning level again.\n", DateTime.Now, systemMemoryPercent); log.Write(tempOutput); log.Close(); }