0

我查阅了有关线程池等的信息,并找到了一个示例。目前我正在尝试重新创建我为自己的项目看到的示例,当我从 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");

        }
    }
}

这是与上述表单程序相关的类。

4

1 回答 1

0

数组索引从零开始,正确的迭代方法是

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

for (int i = 0; i < Write; i++)
{ 
}
于 2012-09-26T04:06:15.360 回答