/// <summary> /// Garbage collection /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tmrCleanup_Tick(object sender, EventArgs e) { if (Settings.Instance != null) { if (MeteredConnection.IsMetered(Settings.Instance.MeteredSSIDs)) { return; } if (Settings.Instance.LastUsage > Settings.Instance.MaxQueries * 0.95) //Don't wipe out if usage is too high. { return; } } if (!cleanupRunning) { try{ CleanupTasks(); //if (tasks.Count == 0) //CleanupFiles(); } catch (Exception) { } } }
private void tmrUpdate_Tick(object sender, EventArgs e) { if (updateRunning) { return; } updateRunning = true; Random r = new Random(); if (Settings.Instance == null) { if (!LoadSettings()) { updateRunning = false; return; } } if (!MeteredConnection.IsMetered(Settings.Instance.MeteredSSIDs)) { Settings.Instance.PurgeCachedResults(new TimeSpan(1, 0, 0)); if (Settings.Instance.CachedResults.Count == 0 || UnderThreshold()) { GetMoreResults(); } } if (Settings.Instance.CachedResults.Count > 0) { int rand = r.Next(0, Settings.Instance.CachedResults.Count - 1); while (Settings.Instance.CachedResults.Count > 0 && !File.Exists(Settings.Instance.CachedResults[rand].Path)) { Settings.Instance.RemoveCachedResult(Settings.Instance.CachedResults[rand]); if (Settings.Instance.CachedResults.Count > 0) { rand = r.Next(0, Settings.Instance.CachedResults.Count - 1); } } if (Settings.Instance.CachedResults.Count > 0) { foreach (Screen screen in Screen.AllScreens) { rand = r.Next(0, Settings.Instance.CachedResults.Count - 1); SetImage(screen, Settings.Instance.CachedResults[rand]); } } else { List <string> files = new List <string>(); files.AddRange(Directory.GetFiles(Settings.Instance.CacheDirectory, "*.jpg")); files.AddRange(Directory.GetFiles(Settings.Instance.CacheDirectory, "*.png")); if (files != null && files.Count > 0) { foreach (Screen screen in Screen.AllScreens) { rand = r.Next(0, files.Count - 1); SetImage(screen, files[rand]); } } } } if (Settings.Instance.UpdateMin != Settings.Instance.UpdateMax) { tmrUpdate.Interval = r.Next(Settings.Instance.UpdateMin * 1000, Settings.Instance.UpdateMax * 1000); } else { tmrUpdate.Interval = Settings.Instance.UpdateMin * 1000; } updateRunning = false; }
/// <summary> /// Timer function to handle Cache and displaying next image. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tmrUpdate_Tick(object sender, EventArgs e) { if (Settings.Instance == null) { if (!LoadSettings()) { lblNotConfigured.Visible = true; if (this.Height - lblNotConfigured.Height > 0 && this.Width - lblNotConfigured.Width > 0) { //lblNotConfigured should move around. lblNotConfigured.Left = ThreadSafeRandom.ThisThreadsRandom.Next(0, this.Width - lblNotConfigured.Width); lblNotConfigured.Top = ThreadSafeRandom.ThisThreadsRandom.Next(0, this.Height - lblNotConfigured.Height); } return; } } else if (lblNotConfigured.Visible) { lblNotConfigured.Visible = false; } if (!Settings.Instance.ShowDescription) { pnlDescription.Visible = false; } if (PlaylistIndex < Playlist.Count) { SetImage(); } try { //Handle stale cache items only if not on a metered WiFi connection. //Not actually tested this yet! if (!MeteredConnection.IsMetered(Settings.Instance.MeteredSSIDs)) { if (Settings.Instance.LastUsage < Settings.Instance.MaxQueries * 0.9 && tasks.Count == 0) //Don't wipe out if usage is too high. { Settings.Instance.PurgeCachedResults(new TimeSpan(1, 0, 0)); } if (Settings.Instance.CachedResults.Count == 0 || UnderThreshold()) { GetMoreResults(); } } if (Settings.Instance.CachedResults.Count > 0) { int rand = ThreadSafeRandom.ThisThreadsRandom.Next(0, Settings.Instance.CachedResults.Count - 1); //Remove Cache item if the image isn't on the file system. (Assume removed externally) while (Settings.Instance.CachedResults.Count > 0 && !File.Exists(Settings.Instance.CachedResults[rand].Path)) { Settings.Instance.RemoveCachedResult(Settings.Instance.CachedResults[rand]); if (Settings.Instance.CachedResults.Count > 0) { rand = ThreadSafeRandom.ThisThreadsRandom.Next(0, Settings.Instance.CachedResults.Count - 1); } } if (Settings.Instance.CachedResults.Count > 0) { rand = ThreadSafeRandom.ThisThreadsRandom.Next(0, Settings.Instance.CachedResults.Count - 1); SetImage(Settings.Instance.CachedResults[rand]); } else { //Fallback if there are images but cache is empty for some reason? List <string> files = new List <string>(); files.AddRange(Directory.GetFiles(Settings.Instance.CacheDirectory, "*.jpg")); files.AddRange(Directory.GetFiles(Settings.Instance.CacheDirectory, "*.png")); if (files != null && files.Count > 0) { rand = ThreadSafeRandom.ThisThreadsRandom.Next(0, files.Count - 1); SetImage(files[rand]); } } this.BackgroundImageLayout = Settings.Instance.Layout; } } catch (Exception) { } if (Settings.Instance.UpdateMin != Settings.Instance.UpdateMax) { tmrUpdate.Interval = ThreadSafeRandom.ThisThreadsRandom.Next(Settings.Instance.UpdateMin * 1000, Settings.Instance.UpdateMax * 1000); } else { tmrUpdate.Interval = Settings.Instance.UpdateMin * 1000; } //if (lblNotConfigured.Text.Equals("OK", StringComparison.InvariantCultureIgnoreCase)) // lblNotConfigured.Visible = false; }