private void Timer_Elapsed(object sender, ElapsedEventArgs e) { var failed = new List <RunningApplication>(); var running = processFactory.GetAllRunning(); var started = running.Where(r => processes.All(p => p.Id != r.Id)).ToList(); var terminated = processes.Where(p => running.All(r => r.Id != p.Id)).ToList(); foreach (var process in started) { logger.Debug($"Process {process} has been started."); processes.Add(process); if (process.Name == "explorer.exe") { HandleExplorerStart(process); } else if (!IsAllowed(process) && !TryTerminate(process)) { AddFailed(process, failed); } else if (IsWhitelisted(process, out var applicationId)) { HandleInstanceStart(applicationId.Value, process); } } foreach (var process in terminated) { logger.Debug($"Process {process} has been terminated."); processes.Remove(process); } if (failed.Any()) { logger.Warn($"Failed to terminate these blacklisted applications: {string.Join(", ", failed.Select(a => a.Name))}."); TerminationFailed?.Invoke(failed); } timer.Start(); }