using System;
using System.Windows.Forms;namespace frm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            System.Threading.Thread thread1 = new System.Threading.Thread(new System.Threading.ThreadStart(this.threadstart));
            thread1.Start();
        }        private void threadstart()
        {
            Common.logView log = new Common.logView();
            log.richTextBox1 = this.richTextBox1;
            log.editText();
        }
    }
}
namespace Common
{
    public class logView
    {
        public RichTextBox richTextBox1 = new RichTextBox();        delegate void delegateSetLogTextBox(string text, bool color);        public void editText()
        {
            this.richTextBox1.Text += "在新线程启动的类中对 RichTextBox.Text 的改写";
        }
    }
}
上面是我的代码,我知道在线程中操作控件需要用 delegate 托管代码......可是我的是在线程启动的类的中操作控件...我在上面的例子中只启动呢一个线程..实际的程序中应该是1个以上的线程..        delegate void delegateSetTextBox(string text);
        private void SetTextBox(string text)
        {
            if (this.richTextBox1.InvokeRequired)
            {
                delegateSetTextBox d = new delegateSetTextBox(this.SetTextBox);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.richTextBox1.AppendText(text);
            }
        }以上的代码..不知道如何用到我上面的代码中......

解决方案 »

  1.   

    在方法  threadstart中加入下列代码:
    private   void   threadstart() 
                    { 
    ....
                            log.delegateSetTextBox = new delegateSetTextBox(SetTextBox);
                    } 
    在类Form1中加入下列方法:
    private   void   SetTextBox(string   text) 
    {
       this.richTextBox1.Text = text;
    }在类logView的方法中进行异步调用
      public   void   editText() 
                    { 
                            object[] datas = { "在新线程启动的类中对   RichTextBox.Text   的改写 "};
                            richTextBox1.Invoke(delegateSetLogTextBox,datas); 
                    } 
      

  2.   

    另:public   RichTextBox   richTextBox1   =   new   RichTextBox(); 
    不必要创建实例,因为richTextBox1 只是对form1的RichTextBox引用
      

  3.   

     多谢  zlc_168   偶去试一下
      

  4.   


    不行呀....==================错误 1
    “delegateSetTextBox”: 无法通过表达式引用类型;请尝试“Common.logView.delegateSetTextBox” D:\USER\My Documents\Visual Studio 2005\Projects\WindowsApplication4\WindowsApplication4\Form1.cs 22 17 Accp.Common===================我的代码...
    using System;
    using System.Windows.Forms;namespace frm
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                System.Threading.Thread thread1 = new System.Threading.Thread(new System.Threading.ThreadStart(this.threadstart));
                thread1.Start();
            }        private void threadstart()
            {
                Common.logView log = new Common.logView();
                log.delegateSetTextBox = new delegateSetTextBox(SetTextBox); 
                log.richTextBox1 = this.richTextBox1;
                log.editText();
            }        delegate void delegateSetTextBox(string text);
        }
    }
    namespace Common
    {
        public class logView
        {
            public RichTextBox richTextBox1;
            public delegate void delegateSetTextBox(string text);        public void editText()
            {
                this.richTextBox1.Text += "在新线程启动的类中对 RichTextBox.Text 的改写";
            }
        }
    }
      

  5.   


    刚写错呢..多谢  zlc_168