public partial class Form1 : Form
    {
        delegate void SetTextCallback(string text);        public void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.label1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.label1.Text = text;
            }
        }        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            Thread runMyADD = new Thread(new ThreadStart(MyAdd));
            runMyADD.Start();
        }        private void MyAdd()
        {
            for (int i = 1; i <= 20; i++)
            {
                SetText(i.ToString());
                Thread.Sleep(100);
            }
        }
    }我想实现线程更新主窗体里的label显示的内容,我现在想把MyAdd写成一个单独的类,不知道应该怎么改,请高手指教,谢谢。

解决方案 »

  1.   

    MS你已经实现第一个了呀,第二个问题很简单吧加上CLASS关键字就可以新建一个类,具体加完后的问题再具体提问吧,
      

  2.   

    public class Add
    {
            public void MyAdd() 
            { 
                for (int i = 1; i <= 20; i++) 
                { 
                    SetText(i.ToString()); 
                    Thread.Sleep(100); 
                } 
            } 
        }
    }
    然后

    Add a = new Add();
                Thread runMyADD = new Thread(new ThreadStart(a.MyAdd)); 
                runMyADD.Start(); 
            } 
      

  3.   

    可是改成类以后,我不知道怎么更改主窗体里的内容
                Class1 runMyADD = new Class1();
                runMyADD.MyADD();   class Class1
        {
            public void MyADD()
            {
                newform.Show();
                for (int i = 1; i <= 20; i++)
                {
                    //这里怎么才能获得form1这个对象呢
                    form1.setlabel = i.ToString();
                    Thread.Sleep(100);
                }
            }
        }
      

  4.   

    写成单独的类以后,没法获得SetText这个方法呀
      

  5.   

    传递一个form对象进去看看不过这个类就只能针对你的窗体来用了
      

  6.   

    SetText方法你也要写到类里面去啊,修改下传递的参数了
      

  7.   

    下面代码,楼主自己调试一下: public partial class Form1 : Form 
    {
      public Form1() 
            { 
                InitializeComponent(); 
            } 
    delegate void DelSetTextCallback(string text);
            public void SetTextCallback(string text)
            {
                this.BeginInvoke(new DelSetTextCallback(SetText), text);
            }
            public void SetText(string text)
            {
                this.label1.Text = text;           
            } 
            private void button1_Click(object sender, EventArgs e)
            {
                ClassA MyAdd = new ClassA();
                MyAdd.SetText = new ClassA.SetTextCallback(SetTextCallback);            Thread runMyADD = new Thread(new ThreadStart(MyAdd.MyADD));
                runMyADD.Start();         }
    }
    //ClassA
    public class ClassA
        {
            public delegate void SetTextCallback(string text);        public SetTextCallback SetText;
            public void MyADD()
            {
                for (int i = 1; i <= 20; i++)
                {
                    SetText(i.ToString());                Thread.Sleep(100);
                }         }
        }