重复的登录脚本

时间:2009-06-17 18:05:43

标签: c# .net network-programming

好的,所以我在C#中写了一个重复的登录“worker”类,我遇到了一些麻烦。我认为,我背后的逻辑是完美的! :(

但是,我不能为我的生活弄清楚为什么它会在第一次出现时触发而不是仅重复:(

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 个答案:

答案 0 :(得分:0)

可能是线程竞争条件。
您在搜索/插入时是否尝试锁定该集合? 我不相信Hashtable是线程安全的。

你可能想要一个

lock(this) {
}

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