问题描述如下:
有一个静态类:
    其中含有一个新建线程的静态方法
有一个窗体类:
    其中含控件若干
现在窗体类中调用静态类的新建线程方法,问静态类中创建的线程入口函数中如何修改该窗体的控件的属性
环境VS2005最好能给个小DEMO  ,  在线等待

解决方案 »

  1.   

     CheckForIllegalCrossThreadCalls = false;//禁用跨线程访问控件异常
      

  2.   

    1: 以下代码都在本窗体内
     
     //代理方法模板
     public delegate void Procedure(); //某个事件中开启一个线程
     Thread thread = new Thread(new ThreadStart(this.StartInit));
     thread.Start();
     
     //线程代理方法, this是指窗体
     public virtual void StartInit(){
          this.Invoke(
               new Procedure(
                  delegate(){
                      this.Update();//这就是跨线程操作
                      this.DialogResult = DialogResult.OK;//比如...
                      this.Text = "信心很重要";//比如改变标题
                  }
                )
          );
          this.Invoke(new Procedure(this.Close));//比比调用关闭窗体的方法
      }2:希望对你有帮助
      

  3.   

    谢谢,//线程代理方法, this是指窗体 
    public virtual void StartInit(){ 
          this.Invoke( 
              new Procedure(                        有点看不懂啊.................
                                                    //////////////////////Procedure代理不是没有参数的吗???

                  delegate(){ 
                      this.Update();//这就是跨线程操作 
                      this.DialogResult = DialogResult.OK;//比如... 
                      this.Text = "信心很重要";//比如改变标题 
                  } 
                ) 
          ); 
          this.Invoke(new Procedure(this.Close));//比比调用关闭窗体的方法 
      }
    能不能给我写个不在同一个类里的例子
      

  4.   

    要用到委托: 
    希望对楼主有所启发 
    delegate void SetTextCallback(string text); 
    private void SetText(string text) 
            { 
                if (this.inforMg.InvokeRequired) 
                { 
                    SetTextCallback d = new SetTextCallback(SetText); 
                    this.Invoke(d, new object[] { text }); 
                } 
                else 
                { 
                    this.inforMg.Text = text; 
                } 
            }
      

  5.   

    利用线程和委托访问窗体控件
    public delegate void  minvole(string str);
     private void button3_Click(object sender, EventArgs e)
            {
                Thread t = new Thread(new ThreadStart(GetTextFun));
                t.Start();
            }private void GetTextFun()
            {
                setText("");
            }
            private void setText(string str)
            {
                if (textBox1.InvokeRequired)
                {
                    minvole _minvole = new minvole(setText);
                    this.Invoke(_myinvole, new object[] { str });
                }
                else
                {
                    this.textBox1.Text = str;
                }
            }
    参考
      

  6.   


    new Procedure(), 内在参数是什么一个实际的方法,你下VS写代码会提示的new Procedure(void() target),
    就是说它的参数就是一个函数指针类似的东东。
    代理和一般类有点特别,建议这方面先了解下!
    请把那段代码写到VS测试,没问题,因为是我项目的源码的一段,我们一向是这样用的!
      

  7.   

    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 exp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                //某个事件中开启一个线程 
                Thread thread = new Thread(new ThreadStart(this.StartInit)); 
                thread.Start();  
            }        //代理方法模板 
            public delegate void Procedure();         //线程代理方法, this是指窗体 
            public virtual void StartInit()
            { 
                this.Invoke( 
                      new Procedure( 
                          delegate(){ 
                              this.Update();//这就是跨线程操作 
                              this.DialogResult = DialogResult.OK;//比如... 
                              this.Text = "信心很重要";//比如改变标题 
                          } 
                        ) 
                  ); 
                  //this.Invoke(new Procedure(this.Close));//比比调用关闭窗体的方法 
              }
        }
    }using System;
    using System.Collections.Generic;
    using System.Text;using System.Threading;namespace exp
    {
        class Class1
        {
            public static Thread myThread;        public static void start()
            {
                myThread=new Thread(new ThreadStart(setText));
                myThread.Start();
            }                private void setText()
            {
                //////////////如何访问form1中的控件
             }
        }
    }