public static void StartService() { Bing bing = null; List <Spotlight> spotlights = null; DateTime startDataTime = new DateTime(); Random random = new Random(); while (!IsDone) { Config.IsChangedFromUser = false; Config config = Config.Load(); Config.IsChangedFromUser = true; imgPaths.Clear(); //找出全部符合文件 foreach (var temp in config.EnableFunctions) { switch (temp) { case 0: //我的 { foreach (var temp2 in config.Wallpapers) { if (temp2.IsFloder) { SearchImg(config, temp2.Path); } else { imgPaths.Add(temp2.Path); } } } break; case 1: //必应 { TimeSpan sp = DateTime.Now.Subtract(startDataTime); if (bing == null || sp.Hours > 3) { bing = BingService.GetWallInfo(config).Result; } if (bing != null) { foreach (var temp2 in bing.Images) { imgPaths.Add(temp2.UrlWithHost); } } } break; case 2: { TimeSpan sp = DateTime.Now.Subtract(startDataTime); if (spotlights == null || sp.Hours > 3) { spotlights = GetSpotlight(); } if (spotlights != null) { foreach (var temp2 in spotlights) { imgPaths.Add(temp2.Path); } } } break; } } if (imgHistory.Count == imgPaths.Count)//已全部出现过,重置 { imgHistory.Clear(); } if (config.Type == 0) { int index = 0; if (imgHistory.Count != 0) { index = imgPaths.IndexOf(imgHistory.Last()) + 1; } SetDestPicture(imgPaths[index]); imgHistory.Add(imgPaths[index]); } else if (config.Type == 1) { int index = -1; do { index = random.Next(0, imgPaths.Count - 1); }while (imgHistory.Contains(imgPaths[index])); SetDestPicture(imgPaths[index]); imgHistory.Add(imgPaths[index]); } Thread.Sleep((int)(config.Duration * 60 * 1000)); } }