线程辅助

时间:2012-09-26 01:14:41

标签: c# threadpool

我已经了解了线程等等,并找到了一个例子。目前我正在尝试重新创建我为自己的项目看到的示例,当我从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");

        }
    }
}

这是连接到上述表单程序的类。

1 个答案:

答案 0 :(得分:0)

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

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

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