using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int i = 0;
        ThreadStart thread = null;
        Thread thread1 = null;
        DataTable dt = null;
        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select top 1000 id,content from articleinfo";
            dt = DBHelper.GetDataSet(sql);
            startThread();
        }
        public void startThread()
        {
            thread = new ThreadStart(addnum);
           
            Thread[] th = new Thread[5];
            for (int j = 0; j < th.Length; j++)
            {
                th[j] = new Thread(thread);
                th[j].IsBackground = true;
                th[j].Start();            }
            thread1 = new Thread(new ThreadStart(DshowNum));
            thread1.IsBackground = true;
            thread1.Start();
         }
        public void addnum()
        {
            foreach (DataRow dr in dt.Rows)
            {
                string id = dr["id"].ToString();
                string content = dr["content"].ToString();
                lock (this)
                {
                    InsertData(id,content);  //  每个线程都会执行一次
                    i++;
                }
            }
                          
        }        public void InsertData(string id,string content)
        {
            try
            {
                string sql = "insert into articleInfo2(id,content)values('" + id + "','" + content + "')";
                DBHelper.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            { }
        }
        public void DshowNum()
        {
            MethodInvoker mi = new MethodInvoker(showNum);
            while (true)
            {
                Invoke(mi);
            }
        }        public void showNum()
        {
            this.label1.Text = "这是第" + i + "个数";
        }
    }
}大侠帮我看一下,在注释的那个方法处,每个线程都会执行一次,怎样才能让其中一个线程访问,别的线程处理下一条数据

解决方案 »

  1.   

    用Monitor, Mutex, Event, 和Semaphore,以同步线程,别的就不多说了。
      

  2.   

    先在主方法里定义好数目:
    int count = dt.Rows.Cout;
    int i = 0;
    然后addnum每次取出一个currentIndex并行处理 public void addnum()
            {       
                while (true)
                {
                    int currentIndex = -1;
                    if (i < count)
                    {
                        lock (this)
                        {
                            if (i < count)
                            {
                                currentIndex = i;
                                i = i + 1;
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (currentIndex != -1)
                        {
                            string id =dt.Rows[currentIndex]["id"].ToString();
                            string content = dt.Rows[currentIndex]["content"].ToString();
                            InsertData(id, content); 
                        }                }
                    else
                    {
                        break;
                    }
                }
            }