2009-06-17 4 views
0

좋아요, 그래서 C#에서 중복 로그온 "작업자"클래스를 작성하고 있는데 약간의 걸림돌을 만났습니다. 그 뒤에있는 나의 논리는 완벽하다고 생각했습니다! 내 인생 파악을 위해 내가 방금보다는를 선두에 트리거 것 수없는 이유 :(중복 로그온 스크립트

하지만, 긴 코드 파일에 대한 :(

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 
    } 
} 

사과를 복제합니다. 내가 돈 ' t 너희들이 나를 도울 수 있도록 붙여 정확히 알고있다./

사전에 감사 :)

답변

0

스레드의 경쟁 조건이 될 수 당신이 검색 할 때
당신이 컬렉션을 잠글 봤어!./그 안에 삽입?
Hashtable이 스레드 안전하다고 생각하지 않습니다.

당신은 해시 테이블을 액세스하는 것을 주위에

lock(this) { 
} 

블록을 넣어 할 수 있습니다.

+0

프로그램의 주 스레드에서 선언 되었기 때문에 해시 테이블에 추가하려고합니다. – Zack