2012-09-26 77 views
0

我查了一下threadpooling等,並找到了它的一個例子。此刻,我正在嘗試重新創建我爲自己的項目看到的示例,並且當我從UI中輸入任何數字時,我不斷收到此錯誤。線程的幫助

ManualResetEvent[] doneReadEvents = new ManualResetEvent[Read]; 
     ManualResetEvent[] doneWriteEvents = new ManualResetEvent[Write]; 
     ReadWrite[] ReadArray = new ReadWrite[Read]; 
     ReadWrite[] WriteArray = new ReadWrite[Write]; 

     for (int i = 0; i < Read; i++) 
     { 
      doneReadEvents[i] = new ManualResetEvent(false); 
      ReadWrite Rw = new ReadWrite(Read, doneReadEvents[i]); 
      ReadArray[i] = Rw; 
      ThreadPool.QueueUserWorkItem(Rw.ThreadPoolCallBackRead, i); 

     } 

     for (int i = 0; i < Write; i++) 
     { 
      doneReadEvents[i] = new ManualResetEvent(false); 
      ReadWrite rW = new ReadWrite(Write, doneWriteEvents[i]); 
      ReadArray[i] = rW; 
      ThreadPool.QueueUserWorkItem(rW.ThreadPoolCallBackWrite, i); 
     } 

     WaitHandle.WaitAny(doneReadEvents); 
     WaitHandle.WaitAny(doneWriteEvents); 
     temp.Items.Add("Complete"); 
     temp.Items.Add("Closing"); 
     Output.DataSource = ReadWrite.MyList; 
     Work.DataSource = ReadWrite.MyList2; 
     ReadWrite.ReadData(Read); 


    } 

第一個循環的第一行出現一個錯誤,說明它超出了數組的範圍。當錯誤清除我不知道會不會有更多的錯誤

namespace MultiThreadingReaderWriter 
{ 
    class ReadWrite 
    { 
     public int _rw; 

     public ManualResetEvent _doneEvents; 

     public List<string> myList = new List<string>(); 
     public List<string> myList2 = new List<string>(); 
     public List<string> MyList{ get { return myList; } } 
     public List<string> MyList2{ get { return myList2; } } 
     public int RW { get { return _rw; } } 


     //Constructor 

     public ReadWrite(int rw, ManualResetEvent doneEvents) 
     { 
      _rw = rw; 

      _doneEvents = doneEvents; 

     } 

     public void ThreadPoolCallBackRead(Object threadContext) 
     { 
      int threadindex = (int) threadContext; 
      myList.Add("Thread Read " + threadindex+ " started"); 
      ReadData(_rw); 
      myList.Add("Thread Read " + threadindex + " done"); 
      _doneReadEvents.Set(); 

     } 
     public void ThreadPoolCallBackWrite(Object threadContext) 
     { 
      int threadindex = (int)threadContext; 
      myList.Add("Thread Write " + threadindex + " started"); 
      WriteData(_rw); 
      myList.Add("Thread Write " + threadindex + " done"); 
      _doneWriteEvents.Set(); 
     } 
     public void ReadData(int reader) 
     { 

      myList.Add("Reader " + reader + " has entered Critical Section"); 
      myList.Add("Reader " + reader + " is Reading"); 
      myList.Add("Reader " + reader + " is leaving Critical Section"); 

     } 

     public void WriteData(int writer) 
     { 

      myList.Add("Writer " + writer + " has entered Critical Section"); 
      myList.Add("Writer " + writer + " is writing"); 
      myList.Add("Writer " + writer + " is leaving Critical Section"); 

     } 
    } 
} 

這是連接到上面的表格程序的類。

回答

0

數組索引從零開始,並重復正確的方法是

for (int i = 0; i < Read; i++) 
{ 
} 

for (int i = 0; i < Write; i++) 
{ 
} 
+0

固定的,你上面提到的,現在我得到一個錯誤的這行代碼的問題'Output.DataSource = ReadWrite.MyList; '說對象引用未設置爲對象的實例。 – imGreg

+0

好像你沒有初始化datasource.you必須做一些像datasource = new List()。檢查你的代碼。 –

+0

我有這個'公開名單 myList =新列表();公開列表 MyList {get {return myList; }}'在類中雖然 – imGreg