最近在做一个项目 要利用到多线程进行多个实例化;实例化里包含实例化新的线程出来;下面是我简单写的个小实例的代码;目前会出现两个问题:1:我的主窗体还是会卡住;我在form1_Load里为程序新进的个子进程;应该不会卡往主线程才对;?????     2:因为程序是隔几秒运行一次 每次都要收新一下数据绑定控件dataGridView1,刷新时就会报错;我在FROM1_LAOD 里用到的 Control.CheckForIllegalCrossThreadCalls = false;控制住刷新控件时的线程问题;但为什么照样在刷新时出现控件错误;
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;            Thread th = new Thread(new ThreadStart(BindThread));
            th.IsBackground = true;
            th.Start();
        }        private void BindThread()
        {
            for (int i = 0; i < 4; i++)
            {
                Gbind gb=new Gbind(this.dataGridView1);
                Thread chlidTh = new Thread(new ThreadStart(Start));
                chlidTh.Start();
            }
        }        public void Start()
        {
            while (true)            {
              List<Person> per = new List<Person>();
              per.Add(new Person("小明",28));
              per.Add(new Person("小阳",20));
              this.dataGridView1.DataSource = per;
              Thread.Sleep(4000);            }
        }
  
          

解决方案 »

  1.   

    推荐使用 BackgroundWorker, 微软写好的一个,百度一下,很好用。
      

  2.   

    Control.CheckForIllegalCrossThreadCalls 只是为了方便诊断线程活动,你现在访问控件的线程不是创建该控件的线程,肯定会报错的,还是用invoke吧
      

  3.   

    控制界面用invoke,不要用CheckForIllegalCrossThreadCalls 
      

  4.   

    跨线程,调用控件了。
    用invoke或返回参数触发事件……
      

  5.   

    楼上的正解,使用invoke挺好用,完全同步.
      

  6.   

    http://blog.csdn.net/wl58796351/article/details/8461397, 这是正解
      

  7.   


    private void BindThread()
             {
                 for (int i = 0; i < 4; i++)
                 {
                     Gbind gb=new Gbind(this.dataGridView1);
                     Thread chlidTh = new Thread(new ThreadStart(Start));
                     chlidTh.Start();
                 }
             }是什么样的需求才使你开4个线程做同一件事情?
      

  8.   


     public Form1()
             {
                 InitializeComponent();
             }         private void Form1_Load(object sender, EventArgs e)
             {
                 Thread th = new Thread(new ThreadStart(BindThread));
                 th.IsBackground = true;
                 th.Start();
             }         private void BindThread()
             {
                 for (int i = 0; i < 4; i++)
                 {
                     Gbind gb=new Gbind(this.dataGridView1);
                     Thread chlidTh = new Thread(new ThreadStart(Start));
                     chlidTh.Start();
                 }
             }         public void Start()
             {
                 while (true)            {
                   List<Person> per = new List<Person>();
                   per.Add(new Person("小明",28));
                   per.Add(new Person("小阳",20));
                   this.Invoke((EventHandler)delegate
                                    {
                                        this.dataGridView1.DataSource = per;
                                    });
                                  Thread.Sleep(4000);            }
             }
    给出了修改的代码,估计这样还是会有问题.不知道你的需求是什么?为什么你要这样实现?