UPusing System;
using System.Drawing;
using System.Text;
using System.Security.Permissions;
using System.Threading;
using System.Windows.Forms;namespace listviewslow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public delegate void AddlistDelegate(string strEntry,  int indexnum);        private void Addlist(string strEntry, int indexnum)
        {            if (InvokeRequired)
            {
                //marshall the call into the main UI thread
                BeginInvoke(new AddlistDelegate(Addlist), new object[] { strEntry,  indexnum });
                return;
            }
           
            ListViewItem item = new ListViewItem(Convert.ToString(indexnum));
            item.SubItems.Add(Convert.ToString(strEntry));
            resultlist.Items.Add(item);        }
        //-------------------
        private void button1_Click(object sender, EventArgs e)
        {
            int id = 1;
            int endid = 1000;
            const int nThreadNumber = 15; //Thread Number            State stateInfo;            int nIndex = 0;
              
            for (int i = 0; i < nThreadNumber; i++)
            {
                if (id == endid)
                    break;
                id++;
                string StrID = id.ToString();
                nIndex++;
                stateInfo =
                    new State(StrID, nIndex );                ThreadPool.QueueUserWorkItem(new WaitCallback(
                    DoSomething), stateInfo);                Thread.Sleep(10);
            }
           //one free, one create
            while (true)
            {
                if (id == endid)
                    break;
                id++;
                string StrID = id.ToString();
                nIndex++;                if (nIndex % 300 == 0)
                {
                    Thread.Sleep(300);
                }
                stateInfo =
                    new State(StrID, nIndex );
                ThreadPool.QueueUserWorkItem(new WaitCallback(
                    DoSomething), stateInfo);
                Thread.Sleep(10);
            }        }
        public void DoSomething(object state)
        {
            // Get parameter            State st = (State)state;
            string StrID = st.strIDNum;
            int nIndex = st.nIndex;
            if (nIndex % 5 == 0)
            {
                Addlist("是5的倍数", nIndex);
            }
            else
            {
                Addlist("不是5的倍数", nIndex);
            }
        }
    }
    public class State
    {
        public string strIDNum;
        public int nIndex;
     
        public State(string strIDNum, int nIndex)
        {
            this.strIDNum = strIDNum;
            this.nIndex = nIndex;
         }
    }}