2009-06-17 101 views
0

好吧,所以我在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 
    } 
} 

道歉长代码文件。我不”知道贴帮你们帮我到底是什么:/

感谢提前:)

回答

0

可能是一个线程竞争条件
你试过锁定集合,当你搜索! /插入它?
我不相信Hashtable是线程安全的。

你可能想要把一个

lock(this) { 
} 

块周围的任何东西访问哈希表。

+0

我调用添加到哈希表,因为它在程序的主线程上声明。 – Zack 2009-06-17 18:13:05