测试代码如下:
(一个按钮,一个TextBox),在TextBox中显示1-1000)
在窗体内的委托测试是成功的。就是DLL中要修改TextBox的值不懂如何写这个委托,请不要告诉我用CheckForIllegalCrossThreadCalls =false,真心求助。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 委托测试2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        //定义委托
        delegate void AppendNumberCallback(string text);
        private void AppendNumber(string text)
        {
            if (this.textBox1.InvokeRequired)
            {
                AppendNumberCallback anc = new AppendNumberCallback(AppendNumber);
                this.textBox1.Invoke(anc, new object[] { text });
            }
            else
            {
                this.textBox1.Text = text;
            }
        }     //按钮事件
        private void button1_Click(object sender, EventArgs e)
        {
            Thread num = new Thread(showNumber);
            num.Start();
        }
        
        //线程
        private void showNumber()
        {
            for (int i = 0; i <= 1000; i++)
            {
                new myshow.Class1().show(this.textBox1, i.ToString());
            }
        }
    }
}
Dll的代码(引用了System.Windows.Forms,以前是将TextBox传递进去,那是在CheckForIllegalCrossThreadCalls=false的情况下用的)using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;namespace myshow
{
    public class Class1
    {
        public Class1()
        {
        }        public void show(TextBox tb1, string _num)
        {
            tb1.Text = _num;
        }
    }
}

解决方案 »

  1.   


    ui线程一定要是调用textbox1的线程
    Class1()是不是当作注入式线程使用了,所以会报错。
      

  2.   

    不知道LZ想要实现什么样的东西,不过你的代码在多线程肯定是有问题的。这样改就可以了。测试窗体代码没变,下面是Dll中的using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using System.Windows.Forms; namespace myshow 

        public class Class1 
        { 
            public Class1() 
            { 
            }         public void show(TextBox tb1, string _num) 
            { 
                //tb1.Text = _num; 
                AppendNumber(tb1, _num);
            }         delegate void AppendNumberCallback(object sender, string text); 
            private void AppendNumber(object sender, string text) 
            {
                TextBox tb = sender as TextBox;
                if (tb == null) return;
                if (tb.InvokeRequired) 
                { 
                    AppendNumberCallback anc = new AppendNumberCallback(AppendNumber); 
                    tb.Invoke(anc, new object[] {sender, text }); 
                } 
                else 
                { 
                    tb.Text = text; 
                }
            } 
        }