/// <summary> /// Used to asyncronously download images. /// </summary> /// <param name="result">The search result to download</param> private void AddImageToCache(Wallpaper result) { try { string path0 = Settings.Instance.CacheDirectory + "\\" + result.id + Path.GetExtension(result.url_image); result.Image.Save(path0); Thread.Sleep(500); //Needed because without it GDI+ may/will throw an exception. result.ClearCachedImages(); //Once saved clear the image to reduce memory. Settings.Instance.AddCachedResults(result, path0); //If nothing is being shown put it up there right away! if (this.BackgroundImage == null && Settings.Instance.CachedResults.Count > 0) { int rand = ThreadSafeRandom.ThisThreadsRandom.Next(0, Settings.Instance.CachedResults.Count - 1); SetImage(Settings.Instance.CachedResults[rand]); Application.DoEvents(); } } catch (Exception ex) { Console.WriteLine("MainForm.AddImageToCache: " + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace); } }