public void DeleteAllSessionIdsNotInList(WTSUniqueSessionIdList _valid_session_ids)
        {
            WTSUniqueSessionIdList kill_list = new WTSUniqueSessionIdList();

            foreach (WTSSession s in this)
            {
                bool kill = true;
                for (int i = 0; i < _valid_session_ids.Count; i++)
                {
                    if (s.UniqueSessionId == _valid_session_ids[i])
                    {
                        kill = false;
                        break;
                    }
                }
                if (kill)
                {
                    kill_list.Add(s.UniqueSessionId);
                }
            }
            if (kill_list.Count > 0)
            {
                this.RemoveSessions(kill_list);
            }
        }
        private void RefreshThread_DoWork(object sender, DoWorkEventArgs e)
        {
            WTSUniqueSessionIdList valid_session_ids = new WTSUniqueSessionIdList();

            foreach (string server in this.WTSServerHostnames)
            {
                IntPtr hServer = IntPtr.Zero;
                try
                {
                    IntPtr SessionInfoPtr = IntPtr.Zero;
                    Int32  sessionCount   = 0;
                    Int32  dataSize       = Marshal.SizeOf(typeof(Jrfc.NativeMethods.WTS_SESSION_INFO));

                    hServer = Jrfc.NativeMethods.WTSOpenServer(server);
                    Int32 retVal = Jrfc.NativeMethods.WTSEnumerateSessions(hServer, 0, 1, ref SessionInfoPtr, ref sessionCount);
                    if (retVal != 0)
                    {
                        Int32 currentSession = (int)SessionInfoPtr;
                        for (int i = 0; i < sessionCount; i++)
                        {
                            Jrfc.NativeMethods.WTS_SESSION_INFO si = (Jrfc.NativeMethods.WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)currentSession, typeof(Jrfc.NativeMethods.WTS_SESSION_INFO));
                            currentSession += dataSize;

                            Jrfc.Wts.WTSSession session = new Jrfc.Wts.WTSSession(hServer, server, si);

                            bool filter = false;
                            if (session.State == NativeMethods.WTS_CONNECTSTATE_CLASS.WTSListen)
                            {
                                filter = true;
                            }
                            else if (session.State == NativeMethods.WTS_CONNECTSTATE_CLASS.WTSDisconnected && string.IsNullOrWhiteSpace(session.Username))
                            {
                                filter = true;
                            }
                            if (!filter)
                            {
                                this.AddOrUpdate(session);
                                valid_session_ids.Add(new WTSUniqueSessionId(server, session.SessionId)); // keep a list of existing sessions
                            }
                        }
                        Jrfc.NativeMethods.WTSFreeMemory(SessionInfoPtr);
                    }
                }
                catch (System.Exception x)
                { Jrfc.Exception.HandleException(x); }
                finally
                { if (hServer != IntPtr.Zero)
                  {
                      Jrfc.NativeMethods.WTSCloseServer(hServer);
                  }
                }
            }
            DeleteAllSessionIdsNotInList(valid_session_ids); // delete (old) sessions that are in the currect session list,
                                                             // but were not in the existing sessions just retreived from the
                                                             // RDS server. Presumably any session not in the valid_session_ids
                                                             // list no longer exists.
            this.m_RefreshThread.ReportProgress(100);
        }