public static void AsyncCallbackImpl(IAsyncResult ar) { string path; try { path = fh.EndInvoke(ar); if (path == null) { Console.WriteLine("Callback() failed to download..."); return; } } catch (Exception e) { Console.WriteLine("Callback() failed to download..."); return; } Console.WriteLine(path + "\t Done..."); SimplePhoto item = new SimplePhoto(); item.path = path; mutex.WaitOne(); if (PicStore.currentPhotos.Count > Setting.iMaxPicNum) { PicStore.Remove(0); } PicStore.currentPhotos.Add(item); PicStore.Save(); mutex.ReleaseMutex(); }
/* * changeWallpaper: timed change wallpaper from photo list */ static void changeWallpaper() { while (true) { int oldPhotoIndex = PicStore.photoIndex; // last photo index mutex.WaitOne(); // get mutex of PicStore.simplePhoto if (PicStore.currentPhotos.Count <= 0) // no photo in lsit { mutex.ReleaseMutex(); Thread.Sleep(Setting.changeFrequent); continue; } if (PicStore.currentPhotos.Count <= PicStore.photoIndex) // photoIndex is too large { PicStore.photoIndex = 0; } else { // get next photo index PicStore.photoIndex = (PicStore.photoIndex + PicStore.currentPhotos.Count - 1) % PicStore.currentPhotos.Count; } // check if exist while (PicStore.Touch(PicStore.photoIndex) == false) { // not exist, change to another if (PicStore.currentPhotos.Count <= 0) { Thread.Sleep(Setting.changeFrequent); continue; } if (PicStore.currentPhotos.Count <= PicStore.photoIndex) { PicStore.photoIndex = PicStore.currentPhotos.Count - 1; } } // check if list is null if (PicStore.currentPhotos.Count <= 0) { mutex.ReleaseMutex(); Thread.Sleep(Setting.changeFrequent); continue; } // absolute path of image String absPath = Environment.CurrentDirectory + @"\pic\" + PicStore.currentPhotos[PicStore.photoIndex].path; mutex.ReleaseMutex(); // release mutex of PicStore.simplePhotos // if index is same, no need change wallpaper if (PicStore.photoIndex != oldPhotoIndex) { Console.WriteLine("ChangeWallpaper() change To " + absPath.Substring(absPath.LastIndexOf('\\') + 1)); SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, absPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE); } // sleep interval time Thread.Sleep(Setting.changeFrequent); } }
//[DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)] //private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow); //[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] //private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); //public static void WindowHide(string consoleTitle) //{ // IntPtr a = FindWindow("ConsoleWindowClass", consoleTitle); // if (a != IntPtr.Zero) // ShowWindow(a, 0);//hide window // else // throw new Exception("can't hide console window"); //} static void Main(string[] args) { //WindowHide(System.Console.Title); Setting.Load(); PicStore.Load(); RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true); key.SetValue(@"WallpaperStyle", 10.ToString()); // fill style new Thread(Download).Start(); new Thread(changeWallpaper).Start(); }