//////////////////////////////////////////////////// // // MONITORING THREADS // //////////////////////////////////////////////////// //Responsible for updating runtimes and running programs //If a program is closed, it will temporarily stop updating for that program public static void monitorRuntimes(object state) { while (!BW_Service._stopping) { Thread.Sleep(100); //Update the running programs list //Check for only those with open window if ((DateTime.Now - _serviceStart).TotalSeconds > FireBaseFunctions.queryComputerTime()) { if (Program.dontShutDown) { if (!_informedOfShutdown) { Console.WriteLine("Not shutting down on request"); _informedOfShutdown = true; } } else { System.Diagnostics.Process.Start("shutdown", "/s /f /t 0"); } } Process[] runningPrograms = Process.GetProcesses().Where(p => !String.IsNullOrEmpty(p.MainWindowTitle)).ToArray(); //Initial add foreach (Process p in runningPrograms) { //If it is not inside the dictionary, add it if (!_programs.ContainsKey(p.ProcessName.ToLower())) { //Query Whitelist from Firebase and store in a dictionary. URL MAY CHANGE Dictionary <string, double> whitelist = JsonConvert.DeserializeObject <Dictionary <string, double> >(FireBaseFunctions.queryList(FireBaseFunctions.getCurrentWhitelistURL() + "/programs.json")); //Modify to be able to ignore case sensitive string name = p.ProcessName.ToLower(); if (!whitelist.ContainsKey(name)) { _programs[p.ProcessName.ToLower()] = new Application(p.ProcessName, p.MainWindowTitle, p.StartTime, 0); } else { _programs[p.ProcessName.ToLower()] = new Application(p.ProcessName, p.MainWindowTitle, p.StartTime, whitelist[name]); } } } //Now loop through active programs, updating the runtimes foreach (KeyValuePair <string, Application> check in _programs) { foreach (Process p in runningPrograms) { //If it is running and the program is in the dictionary, update the runtime if (check.Key.Contains(p.ProcessName.ToLower())) { check.Value.updateRuntime(); } } //If it is not running and the program is in the dictionary, update the startime check.Value.updateStartTime(); } } BW_Service._stoppedEvent.Set(); }
//Static constructor that will have initial running programs static Monitor() { _serviceStart = DateTime.Now; _informedOfShutdown = false; //Initial look at running programs _programs = new Dictionary <string, Application>(); _websites = new Dictionary <string, Website>(); //Check for only those with open window Process[] runningPrograms = Process.GetProcesses().Where(p => !String.IsNullOrEmpty(p.MainWindowTitle)).ToArray(); foreach (Process p in runningPrograms) { //If it is not already inside the list, add it if (!_programs.ContainsKey(p.ProcessName.ToLower())) { //Query Whitelist from Firebase and store in a dictionary. URL MAY CHANGE Dictionary <string, double> whitelist = JsonConvert.DeserializeObject <Dictionary <string, double> >(FireBaseFunctions.queryList(FireBaseFunctions.getCurrentWhitelistURL() + "/programs.json")); //Modify to remove case sensitive when adding to dictionary string name = p.ProcessName.ToLower(); DateTime startTime; try { startTime = p.StartTime; } catch (Exception ex) { startTime = DateTime.Now; } if (!whitelist.ContainsKey(name)) { _programs[p.ProcessName.ToLower()] = new Application(p.ProcessName, p.MainWindowTitle, startTime, 0); } else { _programs[p.ProcessName.ToLower()] = new Application(p.ProcessName, p.MainWindowTitle, startTime, whitelist[name]); } } } }
//Method responsible for returning allocated time in whitelist public static string allocatedTime(string type, string name) { //Check what we are querying, program or website string url; if (type == "website") { url = "/websites.json"; name = name.ToLower().Replace("www.", ""); name = name.Replace('.', '*'); } else { url = "/programs.json"; name = name.ToLower(); } //Query Dictionary <string, string> whitelist = JsonConvert.DeserializeObject <Dictionary <string, string> >(FireBaseFunctions.queryList(FireBaseFunctions.getCurrentWhitelistURL() + url)); if (!whitelist.ContainsKey(name)) { return("no limit set"); } else { return(whitelist[name]); } }
//Method that handles monitoring Blacklist public static void monitorBlacklist(object state) { //Run a loop to continue running service every 15 seconds. CHANGE TIMING LATER while (!BW_Service._stopping) { Thread.Sleep(100); //Query Blacklist Programs from Firebase and store values in a dictionary. URL MAY CHANGE Dictionary <string, double> blacklist = JsonConvert.DeserializeObject <Dictionary <string, double> >(FireBaseFunctions.queryList(FireBaseFunctions.getCurrentBlacklistURL() + "/programs.json")); //Monitor the blacklist //Check for only those with open window Process[] runningPrograms = Process.GetProcesses().Where(p => !String.IsNullOrEmpty(p.MainWindowTitle)).ToArray(); foreach (Process p in runningPrograms) { //Check if a running program is in the blacklist foreach (KeyValuePair <string, double> bs in blacklist) { //If program name is in the blacklist and it is running, kill it if (p.ProcessName.ToLower().Contains(bs.Key)) { p.Kill(); } } } } BW_Service._stoppedEvent.Set(); //END monitorBlacklist Method }