我用一个函数专门来新建线程(比如新建1000或更多的线程)为了能够很好的传参,所以线程执行的函数写为一个类,
在这个类中,要向主窗体的里richtextbox1填入一行字符串,所以问题就来了,这样的方法貌似是不允许的。我要实现的就是点击buttion1后调用函数run(),run()里面就是不断创建线程,线程里所要做的就是填加一段字符串到窗体中的richtextbox1中。但是却无法实现- -!所以我想明白这样的处理方式是否正确?或者是最好的方法是怎么处理?不论怎样,可以安全的实现这个功能就可以
先附上我的处理方法的失败代码 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            run();
        }        public void run()
        {
            for (int i = 0; i < 1500; i++)
            {
                writer wrt=new writer (i.ToString ());
                Thread th = new Thread(new ThreadStart(wrt.Startwrite));
                th.Start();
            }
        }
    }
    class writer
    {
        string aline;
        public writer(string str)
        {
            aline = str;
        }
        public void Startwrite()
        {
            //在这里开始向richTextBox1添加新的一行
        }
    }或许还有改进的方法,或许有更好的方式,期待解答...

解决方案 »

  1.   

    如上,操作UI的组件,不能直接在线程中访问,要通过delegate方式访问
      

  2.   


            class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }            private void button1_Click(object sender, EventArgs e)
                {
                    run();
                }            public void run()
                {
                    for (int i = 0; i < 1500; i++)
                    {
                        writer wrt = new writer(i.ToString());
                        Thread th = new Thread(new ParameterizedThreadStart(wrt.Startwrite));
                        th.Start(this);
                    }
                }            public void SetRichTextBoxText(string str)
                {
                    this.BeginInvoke(new Action(delegate() { this.richTextBox1.Text += str; }));
                }
            }        class writer
            {
                string aline;
                public writer(string str)
                {
                    aline = str;
                }
                public void Startwrite(object form)
                {
                    Form1 f = form as Form1;
                    f.SetRichTextBoxText(aline);
                }
            }
      

  3.   

    额 其实这段代码中的new Action  处在VS2010中说没有传参,要报错 public void SetRichTextBoxText(string str)
                {
                    this.BeginInvoke(new Action(delegate() { this.richTextBox1.Text += str; }));
                }
    不知怎么改,参照着看了一下午的帮助,貌似还是没搞懂。
    还有我对委托还是有点感冒,可不可以帮帮忙写个例子?
      

  4.   


    全部照抄你的然后在这里
     public void SetRichTextBoxText(string str)
                {
                    this.BeginInvoke(new Action(delegate() { this.richTextBox1.Text += str; }));
                }说(new Action(delegate()...的new Action要写为Action<T>..的样子,是不是我要在哪里填上一段代码?  我的软件是VS2010,用.NET2.0。
      

  5.   


    如果是控件的话可能 要CheckForIllegalCrossThreadCalls = false;
      

  6.   

    貌似Action委托只有3.5以上版本才有,如果用2.0的话,只有Action<T>,那你就随便加个泛型参数吧:            public void SetRichTextBoxText(string str)
                {
                    this.BeginInvoke(new Action<bool>(delegate(bool b) { this.richTextBox1.Text += str; }));
                }
    这里的bool你可以换成别的类型
      

  7.   


    这样写上之后,语法的错误是没有了,但是,好像不允许这么做- -...要出异常:在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。这是怎么回事?昨天总是出来这个..My laddygaga   至于加上#10的那句,我也做过,但是.NET2.0不提倡这样的方式,原因是不安全,更加郁闷的是,它居然运行后没有效果- -,我一步一步跟踪过去,它就是没反应...这几天我实在是痛苦啊
      

  8.   

    自己定义一个委托吧……    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            private void button1_Click(object sender, EventArgs e)
            {
                run();
            }        public void run()
            {
                for (int i = 0; i < 1500; i++)
                {
                    writer wrt = new writer(i.ToString());
                    Thread th = new Thread(new ParameterizedThreadStart(wrt.Startwrite));
                    th.Start(this);
                }
            }        public void SetRichTextBoxText(string str)
            {
                this.BeginInvoke(new MyDelegate(delegate() { this.richTextBox1.Text += str; }));
            }
        }    public delegate void MyDelegate();    class writer
        {
            string aline;
            public writer(string str)
            {
                aline = str;
            }
            public void Startwrite(object form)
            {
                //在这里开始向richTextBox1添加新的一行
                Form1 f = form as Form1;
                f.SetRichTextBoxText(aline);
            }
        }这段代码我已经试过,应该没问题了,不过由于启动的线程过多,所以会比较慢,你先把线程数减少一些看看效果