【问题标题】:Threadpooling assistance线程池帮助
【发布时间】:2012-09-26 15:19:10
【问题描述】:

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

        }
    }
}

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

【问题讨论】:

    标签: c# threadpool


    【解决方案1】:

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

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

    【讨论】:

    • 修复了你上面提到的那个问题,现在我收到这行代码的错误Output.DataSource = ReadWrite.MyList; 说对象引用未设置为对象的实例。
    • 好像你还没有初始化数据源。你必须做一些类似 datasource= new List() 的事情。检查你的代码。
    • 我在课堂上有这个public List&lt;string&gt; myList = new List&lt;string&gt;(); public List&lt;string&gt; MyList{ get { return myList; } }
    • 什么是 output.datasource?数据源是列表 对象
    • output 是列表控件的名称,DataSource 是 ListControl.DataSource 中的一个对象,用于获取或设置此 system.windows.forms.listcontrol 的数据源,我刚刚意识到关于 output.datasource 的部分不在原来的帖子里,我现在就把它放在那里
    猜你喜欢
    • 1970-01-01
    • 2012-09-27
    • 2012-04-06
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多