2009-06-17 74 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