public object GetDesktopByIndex(IntPtr hMon, int index)
 {
     return(ExecuteWithRetry(() => m_vds.GetDesktopByIndex(hMon, index)));
 }
        void IWin32VirtualDesktopManagerInternal.CheckVirtualDesktopChanges()
        {
            List <Win32VirtualDesktop> removedDesktops = new List <Win32VirtualDesktop>();
            List <Win32VirtualDesktop> addedDesktops   = new List <Win32VirtualDesktop>();

            int newDesktopCount = m_vds.GetDesktopCount(m_hMon);
            int oldDesktopCount;

            lock (m_syncRoot)
            {
                oldDesktopCount = m_desktops.Count;
                if (oldDesktopCount > newDesktopCount)
                {
                    for (int i = newDesktopCount; i < oldDesktopCount; i++)
                    {
                        removedDesktops.Add(m_desktops[i]);
                    }
                    m_desktops.RemoveRange(oldDesktopCount - 1, oldDesktopCount - newDesktopCount);
                }
                else if (oldDesktopCount < newDesktopCount)
                {
                    for (int i = oldDesktopCount; i < newDesktopCount; i++)
                    {
                        var newInstance = new Win32VirtualDesktop(m_workspace, m_vds, m_vds.GetDesktopByIndex(m_hMon, i));
                        m_desktops.Add(newInstance);
                        addedDesktops.Add(newInstance);
                    }
                }
            }

            List <Exception> exs = new List <Exception>();

            foreach (var desktop in removedDesktops)
            {
                try
                {
                    desktop.OnRemoved();
                }
                catch (Exception e)
                {
                    exs.Add(e);
                }
                finally
                {
                    try
                    {
                        DesktopRemoved?.Invoke(this, new DesktopChangedEventArgs(desktop));
                    }
                    catch (Exception e)
                    {
                        exs.Add(e);
                    }
                }
            }

            foreach (var desktop in addedDesktops)
            {
                try
                {
                    DesktopAdded?.Invoke(this, new DesktopChangedEventArgs(desktop));
                }
                catch (Exception e)
                {
                    exs.Add(e);
                }
            }

            if (exs.Count > 0)
            {
                throw new AggregateException(exs);
            }

            int             newCurrentDesktop = m_vds.GetCurrentDesktopIndex(m_hMon);
            int             oldCurrentDesktop;
            IVirtualDesktop newDesktop;
            IVirtualDesktop?oldDesktop;

            lock (m_syncRoot)
            {
                oldCurrentDesktop = m_currentDesktop;
                oldDesktop        = oldCurrentDesktop < m_desktops.Count ? m_desktops[oldCurrentDesktop] : null;
                if (newCurrentDesktop != oldCurrentDesktop)
                {
                    m_currentDesktop = newCurrentDesktop;
                }
                newDesktop = m_desktops[newCurrentDesktop];
            }

            if (oldCurrentDesktop != newCurrentDesktop)
            {
                CurrentDesktopChanged?.Invoke(this, new CurrentDesktopChangedEventArgs(newDesktop, oldDesktop));
            }
        }