以前没有接触过多线程,想要个例子研究一下;哪位给点示代码提示;
winform中界面:一个datagridview控件,控件id为默认,用来记录所有线程的状态;一个开始按钮,一个停止按钮,一个自动执行按钮
功能要求:
点击开始按钮,多线程执行一次,停止按钮停止所有正在执行的线程。自动执行按钮让所有线程自动执行;
下面是我写的代码,有些不对劲,gridview的数据没有实时更新。望高手指点;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 ThreadControlView
{
    public delegate void upview();//线程委托
    public partial class Form2 : Form
    {
        private int threadNum = 10;//线程总数
        private Thread[] thread = null;
        public Form2()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 创建一个datatable,用来记录线程状态
        /// </summary>
        private void bd_view()
        {
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("id");
            dt.Columns.Add(dc);
            dc = new DataColumn("stats");
            dt.Columns.Add(dc);
            dc = new DataColumn("msg");
            dt.Columns.Add(dc);
            for (int i = 0; i < threadNum; i++)
            {
                DataRow dr = dt.NewRow();
                dr["id"] = (i + 1).ToString();
                dr["stats"] = "停止";
                dr["msg"] = "Thread" + (i + 1).ToString();
                dt.Rows.Add(dr);
            }
            dataGridView1.DataSource = dt;
        }        private void Form2_Load(object sender, EventArgs e)
        {
            bd_view();
        }        private void modi()
        {
            if (!dataGridView1.InvokeRequired)
            {                getModi();
            }
            else
            {
                dataGridView1.Invoke(new upview(getModi));
            }
        }
        /// <summary>
        /// void,让threadstart调用
        /// </summary>
        private void getModi()
        {
            if (thread[commandVar.num] != null)
            {
                modifyStats(commandVar.num, thread[commandVar.num].ThreadState.ToString(), thread[commandVar.num].Name);
            }
        }
        /// <summary>
        /// 更新gridview中指定行的数据,给getModi()调用
        /// </summary>
        /// <param name="index"></param>
        /// <param name="threadstats"></param>
        /// <param name="threadname"></param>
        private void modifyStats(int index,string threadstats,string threadname)
        {
            dataGridView1.Rows[index].Cells[1].Value = threadstats;
            dataGridView1.Rows[index].Cells[2].Value = threadname;
            Thread.Sleep(100);
        }
        /// <summary>
        /// 多线程自动运行
        /// </summary>
        private void autoRun()
        {
            thread = new Thread[threadNum];
            for (int i = 0; i < threadNum; i++)
            {
                Thread t = new Thread(new ThreadStart(modi));
                thread[i] = t;
                thread[i].Name = "Thread" + (i + 1).ToString();
                thread[i].Start();
            }
            int curnum = 0;//计数器
            while (commandVar.flag == 0)
            {                   modi();
                Thread.Sleep(100);
                curnum++;
                if (curnum > commandVar.max)
                    break;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            commandVar.flag = 0;
            upview up = new upview(getModi);
            thread = new Thread[threadNum];
            
            for (int i = 0; i < threadNum; i++)
            {
                Thread t = new Thread(new ThreadStart(modi));
                thread[i] = t;
                thread[i].Name = "Thread" + (i + 1).ToString();
                thread[i].Start();
                modifyStats(i, thread[i].ThreadState.ToString(), thread[i].Name);
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            commandVar.flag = 1;
        }        private void button3_Click(object sender, EventArgs e)
        {
            autoRun();
        }
    }
    public class commandVar
    {
        private static int _num=0;//当前进程序号
        private static int _flag = 0;//是否运行
        private static int _max = 10;//最高运行次数
        public static int num
        {
            set { _num = value; }
            get { return _num; }
        }
        public static int flag
        {
            set { _flag = value; }
            get { return _flag; }
        }
        public static int max
        {
            set { _max = value; }
            get { return _max; }
        }
    }
}

解决方案 »

  1.   

    写的时候没有注意这些,看了这个博客上的来做的,才知道要改变控件的值要用到委托;
    大家可以不看我的代码。
    简单的说,我的要求,10个线程循环执行十轮,执行过程中,用datagridview实时来显示这十个线程的状态和名称;
      

  2.   

    怎么没有高手来回一下呢?
    我的功能要求是:10个线程循环执行十轮,执行过程中,用datagridview实时来显示这十个线程的状态和名称
    只要是c#代码就可以,大家想怎么写就怎么写。不要看我的代码。