Thread Pool read List <T> instance, thread list
Private static readonly Object ThisLock = new object (); private static readonly AutoResetEvent AutoReset = new AutoResetEvent (false); private static int _ selectindex; private const int Threadcount = 10; private int _ finishcount; List <T> _ datamodellist; public void LoadPage () {Grid ();} private void Grid (){
_ List. clear (); _ finishcount = 0; _ selectindex = 0; _ datamodellist = GetAll (); for (var I = 0; I <Threadcount; I ++) {var thread = new Thread (DoTask); thread. start (1);} AutoReset. waitOne (); // wait for the thread pool to be completed // do something} public void DoTask (Object index) {while (_ selectindex <_ datamodellist. count) {lock (ThisLock) // after locking, only one thread enters until the code in the block is executed {if (_ selectindex> = _ datamodellist. count) continue; daimamodel = _ datamodellist [_ selectindex]; _ selectindex ++;} // do something _ finishcount ++; if (_ finishcount = _ datamodellist. count) {AutoReset. set (); // terminate after the last thread is completed
}
}
}