private void _resetTimer_Elapsed(object sender, ElapsedEventArgs e) { try { DateTime resetTime = Convert.ToDateTime("01:00"); TimeSpan t1 = resetTime.Subtract(resetTime.Date); TimeSpan t2 = DateTime.Now.TimeOfDay; if ((t1.Hours == t2.Hours) && (t1.Minutes == t2.Minutes)) { _processStartEvent.Stop(); _processStopEvent.Stop(); _eventTimer.Stop(); _eventKillTimer.Stop(); Common.DeleteDataFile(); ProcessesToCheck.Clear(); _processStartEvent.Start(); _processStopEvent.Start(); _eventTimer.Start(); _eventKillTimer.Start(); } } catch (Exception ex) { Common.WriteErrorToLog(ex); } }
private void ProcessSearch() { var procList = Process.GetProcesses(); var processes = new List <Process>(procList); var processesToFind = Common.AppSettings.Settings["ProcessName"].Value.Split(';'); foreach (var procName in processesToFind) { var processesExist = processes.FindAll(p => p.ProcessName.Equals(procName.Replace(".exe", ""), StringComparison.InvariantCultureIgnoreCase)); if (processesExist.Any()) { foreach (var proc in processesExist) { var processName = proc.ProcessName; var processId = proc.Id; ProcessesToCheck.TryGetValue(processName, out ProcessDetails processDetails); if (processDetails == null) { processDetails = new ProcessDetails { Name = processName, Pid = processId, StartTime = DateTime.Now }; } // var processDetails = new ProcessDetails { Name = processName, Pid = processId, StartTime = DateTime.Now }; var totalTime = processDetails.TotalTime; Console.WriteLine("Total Time: {0} Allowed Total Time:", totalTime, AllowedTotalTime); if (totalTime >= AllowedTotalTime) { // not allowed to continue playing today.. if (!_processToKill.Contains(processDetails.Pid)) { Common.WriteToLog(string.Format("Process exceeded allowed time : {0} will close at {1}", totalTime, DateTime.Now.AddMinutes(5))); ShowInfoMessage(); _processToKill.Add(processDetails.Pid); // proc.Kill(); } } else { Common.WriteToLog(string.Format("Process [{0}] is currently running | ID: {1}", processName, processId)); ProcessesToCheck.AddOrUpdate(processName, processDetails, (newKey, oldValue) => processDetails); } } } else { Common.WriteToLog("process not found at startup"); } } }
private void _eventKillTimer_Elapsed(object sender, ElapsedEventArgs e) { _eventKillTimer.Stop(); if (_processToKill.Count > 0) { foreach (var proc in _processToKill) { var procName = ""; try { var process = Process.GetProcessById(proc); if (process != null) { procName = process.ProcessName; process.Kill(); } } catch (ArgumentException) { } catch (Exception ex) { Common.WriteErrorToLog(ex); } var msg = string.Format("Process name [{0}] id:{1} killed", procName, proc); SendMailNotification.SendMail(msg, ""); Common.WriteToLog(msg); ProcessDetails processDetails; ProcessesToCheck.TryRemove(procName, out processDetails); Common.WriteToLog(string.Format("Process: {0} removed from checking list", proc)); // kill IE to lcose all messages var ieProcs = Process.GetProcessesByName("IEXPLORE"); foreach (Process ieProc in ieProcs) { if (ieProc.MainWindowTitle.IndexOf("msg.html") > -1) { ieProc.Kill(); } } } _processToKill.Clear(); } _eventKillTimer.Start(); }
private void OnTimerEvent(object sender, ElapsedEventArgs e) { try { _eventTimer.Stop(); Common.WriteToLog(string.Format("Checking games... next check: {0}", DateTime.Now.AddMinutes(1))); if (e.SignalTime >= _eventTimerTime && ProcessesToCheck.Count > 0) { foreach (var processDetailse in ProcessesToCheck) { var process = GetProcess(processDetailse.Value.Pid); if (process != null) { var totalTime = DateTime.Now - process.StartTime; Console.WriteLine("Total Time : {0}", totalTime); UpdateProcessTimeStamp(processDetailse.Value.Name, false, true); var aggrigateTime = ProcessesToCheck.Sum(p => p.Value.TotalTime); if (aggrigateTime >= AllowedTotalTime) { if (!_processToKill.Contains(processDetailse.Value.Pid)) { Common.WriteToLog(string.Format("Process exceeded allowed time : {0} will close at {1}", aggrigateTime, DateTime.Now.AddMinutes(5))); ShowInfoMessage(); _processToKill.Add(processDetailse.Value.Pid); _eventKillTimer.Start(); } } } } _eventTimerTime = DateTime.Now.AddMinutes(1); } } catch (Exception ex) { Common.WriteToLog(ex.ToString()); } finally { _eventTimer.Start(); } }