怎么在线程结束的时候给textbox赋值,我的界面是这样的。是多个线程,这里我只写了一个。线程指向的方法写在一个类里面。     
 if (!thread.IsAlive) 
            {
                this.Invoke((EventHandler)(delegate//timer控件启动线程
                {
                    textBox3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                }));
            }

解决方案 »

  1.   

    线程函数中最后面写上你的那个invoke不就行了?
      

  2.   

    用 BackgroundWorker这个吧,比较容易操作。
    完成后有个事件RunWorkerCompleted 。这把代码写这就行了。
      

  3.   

    参考这个:http://bbs.csdn.net/topics/390162519
      

  4.   

    有两种方法:
    一是把 TextBox 传到线程中去,然后用 TextBox.Invoke 显示时间
    二是把没信号的 AutoResetEvent 传到线程中去,然后用 AutoResetEvent.Set() 设为有信号,在主窗体判断是 if(AutoResetEvent.WaitOne(0)) { 显示时间,删除 AutoResetEvent}
      

  5.   

    Invoke就是看不到效果吧,beginInvoke呢?
      

  6.   

    Invoke就是看不到效果吧,beginInvoke呢?还是不行,我的线程查询的数据库量很大。
      

  7.   

    点击手动加载数据时调用这个方法。在所有线程结束的时候给“本次结束时间”跟“最后同步时间”的文本框赋值,我能调试到线程关闭,但是就是没有赋值。
    public void Data_insert()
            {
                Thread tr = null;
                Thread swr = null;
                Thread srr = null;
                Thread str = null;
                Thread spr = null;
                Thread fs= null;
                Thread fl = null;
                Thread fx= null;
                string[] type = { "ST_RIVER_R", "ST_WAS_R", "ST_RSVR_R", "ST_TIDE_R", "ST_PPTN_R", "" };
                for (int i = 0; i < type.Length; i++)
                {
                    switch (type[i])
                    {                    case "ST_RIVER_R":   //河道。
                            {
                                try
                                {
                                    tr = new Thread(new ParameterizedThreadStart(ST_RIVER_R));
                                    tr.Name = "ST_RIVER_R";
                                    tr.IsBackground = true;
                                    tr.Start(listBox1);
                                }
                                catch (Exception)
                                {
                                    throw;
                                }
                                break;
                            }
                        case "ST_WAS_R":   //闸坝。
                            {
                                try
                                {
                                    swr = new Thread(new ParameterizedThreadStart(ST_WAS_R));
                                    swr.IsBackground = true;
                                    swr.Start(listBox1);
                                }
                                catch (Exception)
                                {
                                    throw;
                                }
                                break;
                            }
                        case "ST_RSVR_R":   //水库。
                            {
                                try
                                {
                                    srr = new Thread(new ParameterizedThreadStart(ST_RSVR_R));
                                    srr.IsBackground = true;
                                    srr.Start(listBox1);
                                }
                                catch (Exception)
                                {                                throw;
                                }
                                break;
                            }
                        case "ST_TIDE_R":   //潮位。
                            {
                                try
                                {
                                    str = new Thread(new ParameterizedThreadStart(ST_TIDE_R));
                                    str.IsBackground = true;
                                    str.Start(listBox1);
                                }
                                catch (Exception)
                                {                                throw;
                                }
                                break;
                            }
                        case "ST_PPTN_R":
                            {
                                try
                                {
                                    spr = new Thread(new ParameterizedThreadStart(ST_PPTN_R));
                                    spr.IsBackground = true;
                                    spr.Start(listBox1);
                                }
                                catch (Exception)
                                {                                throw;
                                }                            break;
                            }
                        default:
                            {
                                try
                                {
                                    fs = new Thread(new ParameterizedThreadStart(fengSpeed));
                                    fs.IsBackground = true;
                                    fs.Start(listBox1);                                fl = new Thread(new ParameterizedThreadStart(fengLi));
                                    fl.IsBackground = true;
                                    fl.Start(listBox1);                                fx = new Thread(new ParameterizedThreadStart(fengXiang));
                                    fx.IsBackground = true;
                                    fx.Start(listBox1);
                                }
                                catch (Exception)
                                {                                throw;
                                }
                                break;
                            }
                    }
                }
                if ((!tr.IsAlive) && (!swr.IsAlive) && (!srr.IsAlive) && (!str.IsAlive) && (!spr.IsAlive) && (!fs.IsAlive) && (!fl.IsAlive) && (!fx.IsAlive))
                {
                    this.Invoke((EventHandler)(delegate//timer控件启动线程
                    {
                        //textBox3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        textBox2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        SetText3("最后同步时间为:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    }));
                }
                //this.BeginInvoke((EventHandler)(delegate { //异步
                    
                //}));
            }