代码之家  ›  专栏  ›  技术社区  ›  Zack

重复登录脚本

  •  0
  • Zack  · 技术社区  · 17 年前

    但是,我一辈子都搞不懂为什么它会在第一次发生时触发而不是在重复发生时触发:(

    namespace Lab.Core.BackgroundWorker {
        using Lab.Core;
        using Lab.Core.Net;
    
        using System;
        using System.Collections;
        using System.Collections.Generic;
        using System.Text;
        using System.Threading;
        using System.Windows.Forms;
    
        public class MultiLogon : IWorker {
            private static Hashtable LoggedOnUsers = new Hashtable();
            private Thread _worker = null;
            //private Thread m_UsersUpdate = null;
    
            public delegate Boolean AddUserToCollectionDelegate(String user, String computer);
            public delegate void ClearCollectionDelegate(String user);
            public delegate Boolean IsUserLoggedInDelegate(String user);
    
            public Boolean AddUserToCollection(String user, String computer) {
                int retVal = MultiLogon.LoggedOnUsers.Count + 1;
                if (String.IsNullOrEmpty(user) || String.IsNullOrEmpty(computer))
                    return false;
                if (!MultiLogon.LoggedOnUsers.ContainsKey(user))
                    MultiLogon.LoggedOnUsers.Add(user, computer);
    
                return (MultiLogon.LoggedOnUsers.Count == retVal);
            }
    
            public void ClearCollection() {
                if (MultiLogon.LoggedOnUsers.Count > 0)
                    MultiLogon.LoggedOnUsers.Clear();
            }
    
            public Boolean IsUserLoggedIn(String user) {
                if (String.IsNullOrEmpty(user))
                    return false;
                return (LoggedOnUsers.Contains(user));
            }
    
            #region IWorker Members
    
            public void Run(object obj) {
                AddUserToCollectionDelegate add = new AddUserToCollectionDelegate(AddUserToCollection);
                //ClearCollectionDelegate clear = new ClearCollectionDelegate(ClearCollection);
                //IsUserLoggedInDelegate isLogged = new IsUserLoggedInDelegate(IsUserLoggedIn);
    
                while (true) {
                    foreach (Computer c in ComputerList.Instance)
                        if (!add.Invoke(c.UserName, c.MachineName)) {
                            // duplicate! or not? :/
                            // Credit (through adoption of code) goes to:
                            //  http://bytes.com/groups/net-c/263778-quickly-finding-duplicates-arraylist#post1059834
                            foreach (DictionaryEntry item in MultiLogon.LoggedOnUsers) {
                                MessageBox.Show((String)item.Key, (String)item.Value);
                                //NetworkMessage.Send((String)item.Value, String.Format("It is against lab policy to share your account with anyone other than yourself or use someone else's account! Logout immediately or further action will be taken. Your action has been logged."));
                                //OffenseManager.Instance.AddOffense((String)item.Key, null, String.Format("Account sharing - Computer: {0}", item.Value), false);
                            }
                        }
                    Thread.Sleep(750);
                }
            }
    
            public void Start() {
                _worker = new Thread(new ParameterizedThreadStart(Run));
                _worker.IsBackground = true;
                _worker.Start();
            }
    
            public void Stop() {
                if (_worker.IsAlive)
                    _worker.Abort();
            }
    
            #endregion
        }
    }
    

    为长代码文件道歉。我不知道该贴什么来帮你们。:/

    提前谢谢!:)

    1 回复  |  直到 11 年前
        1
  •  0
  •   Andrew Arace    17 年前

    可能是线程争用条件。
    在搜索/插入收藏时,是否尝试过将其锁定?
    我不相信哈希表是线程安全的。

    lock(this) {
    }
    

    阻止访问哈希表的任何内容。