private static void WatchDesktop() { while (true) { try { using (var inputDesktop = new Desktop()) { inputDesktop.OpenInput(); if (!inputDesktop.DesktopName.Equals(_lastDesktop)) { if (inputDesktop.Show() && Desktop.SetCurrent(inputDesktop)) { Console.WriteLine($"Desktop switched from {_lastDesktop} to {inputDesktop.DesktopName} on thread {Desktop.GetCurrentThreadId()}"); _lastDesktop = inputDesktop.DesktopName; _lastDesktopInput = inputDesktop; CurrentDesktop = inputDesktop; } else { var errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message; Console.WriteLine(errorMessage); _lastDesktopInput?.Close(); } } } } catch (Exception ex) { Console.WriteLine(ex.Message, ex); } Thread.Sleep(1000); } }
/// <summary> /// Switches to the specified desktop. /// </summary> /// <param name="name">Name of desktop to switch input to.</param> /// <returns>True if desktops were successfully switched.</returns> public static bool Show(string name) { // attmempt to open desktop. var result = false; using (var d = new Desktop()) { result = d.Open(name); // something went wrong. if (!result) { return(false); } // attempt to switch desktops. result = d.Show(); } return(result); }
/// <summary> /// Switches to the specified desktop. /// </summary> /// <param name="name">Name of desktop to switch input to.</param> /// <returns>True if desktops were successfully switched.</returns> public static bool Show(string name) { // attmempt to open desktop. var result = false; using (var d = new Desktop()) { result = d.Open(name); // something went wrong. if (!result) return false; // attempt to switch desktops. result = d.Show(); } return result; }