FromId() публичный статический Метод

Returns the virtual desktop of the specified identifier.
public static FromId ( System.Guid desktopId ) : VirtualDesktop
desktopId System.Guid
Результат VirtualDesktop
Пример #1
0
        static IDisposable RegisterMinimalListener()
        {
            var window = new TransparentWindow();

            window.Show();
            Guid?        desktopId  = null;
            var          timeLimit  = TimeSpan.FromSeconds(30);
            var          limitTimer = Stopwatch.StartNew();
            COMException exception  = null;
            int          attempts   = 10;

            while (limitTimer.Elapsed < timeLimit || attempts > 0)
            {
                attempts = Math.Max(0, attempts - 1);
                try {
                    desktopId = VirtualDesktop.IdFromHwnd(window.Handle);
                    exception = null;
                } catch (COMException ex) when(ex.Match(HResult.INVALID_STATE))
                {
                    Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new Action(() => {}));
                    if (limitTimer.Elapsed >= timeLimit && attempts <= 0)
                    {
                        throw;
                    }
                    exception = ex;
                }
            }

            if (desktopId == null && exception != null && limitTimer.Elapsed >= timeLimit)
            {
                throw exception;
            }

            var timer = new DispatcherTimer(DispatcherPriority.Normal)
            {
                Interval  = TimeSpan.FromMilliseconds(250),
                IsEnabled = true,
            };

            timer.Tick += delegate {
                var newId = VirtualDesktop.IdFromHwnd(window.Handle);
                if (newId == null || newId == desktopId)
                {
                    return;
                }
                var newDesktop  = VirtualDesktop.FromId(newId.Value);
                var oldDesktop  = desktopId == null ? null : VirtualDesktop.FromId(desktopId.Value);
                var changedArgs = new VirtualDesktopChangedEventArgs(oldDesktop, newDesktop);
                CurrentChanged?.Invoke(typeof(VirtualDesktop), changedArgs);
                desktopId = newId;
            };

            return(Disposable.Create(() => {
                timer.Stop();
                window.Close();
            }));
        }
Пример #2
0
 static VirtualDesktop FromComObject(IVirtualDesktop virtualDesktop) =>
 IsSupported
                             ? VirtualDesktop.FromComObject(virtualDesktop)
                             : VirtualDesktop.FromId(virtualDesktop.GetID());