在Form1.cs中开始线程:
        private void button1_Click(object sender, EventArgs e)
        {
            Thread[] th = new Thread[10];
            for (int i = 0; i < ThreadNum; i++)
            {
                 th[i] = new Thread(new ThreadStart(pro.RequestProc));
                 th[i].Start();
            }
            
        }        public void showMsg(string msg)
        {
            MessageBox.Show("测试是否委托传入"+msg);
            if (this.listBox1.InvokeRequired)
            {
                if (this.listBox1.Items.Count < 20)
                {
                    this.listBox1.Items.Add(msg);
                }
                else
                {
                    this.listBox1.Items.RemoveAt(0);
                    this.listBox1.Items.Add(msg);
                }
            }
        }在Processer.cs中:
  public delegate void MydeleGate(string msg);
  public class Processer
  {
        public MydeleGate mydel;
        Form1 oForm = new Form1();
        public void RequestProc()
        {
             ..................
             string a = "haha";
             mydel = new MydeleGate(oForm.showMsg);
             mydel(a);
                  
        }
  }我现在能够在showMsg里接收到msg的值,但是就是显示不出来!求帮忙!今天刚学委托,实在不行了!谢谢!30分!

解决方案 »

  1.   

    我改了一下,老兄你看一看,调试通过
    private void button1_Click(object sender, System.EventArgs e)
    {
    Processer pro=new Processer(this);
    Thread[] th = new Thread[5];
    for (int i = 0; i < th.GetLongLength(0); i++)
    {
    th[i] = new Thread(new ThreadStart(pro.RequestProc));
    th[i].Start();
    }
    }
    public void showMsg(string msg)
    {
    MessageBox.Show("测试是否委托传入"+msg);

    if (this.listBox1.InvokeRequired)
    {
    if (this.listBox1.Items.Count < 20)
    {
    this.listBox1.Items.Add(msg);
    }
    else
    {
    this.listBox1.Items.RemoveAt(0);
    this.listBox1.Items.Add(msg);
    }
    }
    //主要是这里
    public class Processer
    {
             public delegate void MydeleGate(string msg);
             public Form1 frm;
    public MydeleGate mydel;
    public Processer(Form1 f)
    {
           frm=f;
    }

    public void RequestProc()
    {

             string a = "haha";
    mydel = new MydeleGate(frm.showMsg);
    mydel(a);
                      
    }
    }
      

  2.   

    .Net20中需要在楼上面的基础上把showMsg函数改成下面这样: 
    public void showMsg(string msg)
            {
               // MessageBox.Show("测试是否委托传入" + msg);
                if (this.listBox1.InvokeRequired)
                {
                    MydeleGate  d = new MydeleGate(showMsg);
                    this.Invoke(d, new object[] { msg });
                }
                else
                {                if (this.listBox1.Items.Count < 20)
                    {
                        this.listBox1.Items.Add(msg);
                    }
                    else
                    {
                        this.listBox1.Items.RemoveAt(0);
                        this.listBox1.Items.Add(msg);
                    }
                }
            }
      

  3.   

    谢谢小新,但出现这个错误:
    线程间操作无效: 从不是创建控件“listBox1”的线程访问它。
      

  4.   

    刚刚调试成功了!
    不过把
    public delegate void MydeleGate(string msg);放在了
    public class Processer上面加上jointan() 老兄的已经成功了!~谢谢两位!~不过这个原理还不是很懂!!一定要在构造的时候加进去吗?
      

  5.   

    能对Processer中的oForm赋值就可以了,没必要放到构造函数中,但必须把this赋值给oForm,而不是赋一个new Form1();
      

  6.   

    我用的是如下方法: 
    Processer pro = new Processer();
                pro.oForm = this;
                Thread[] th = new Thread[10];
                for (int i = 0; i < 10; i++)
                {
                    th[i] = new Thread(new ThreadStart(pro.RequestProc));
                    th[i].Start();
                }
    /////////////////////////////////////////////////////
     public class Processer
        {
            public MydeleGate mydel;
            public Form1 oForm = null;这句我临时改成了public
            public void RequestProc()
            {
                // ..................
                 string a = "haha";
                 mydel = new MydeleGate(oForm.showMsg);
                 mydel(a);
                      
            }
        }
      

  7.   

    这个问题关键不在于你的Processer如何定义,而是在于改变控件属性的方法必须判断是不是创建自己的线程
    一但InvokeRequired为真的话,说明执行方法的线程不属于创建控件的线程,那么就需要把这个方法"外包"给一个使用Invoike的执行步骤去执行,而且按Invoike的规定,必须把要执行的函数包装成一个委托.
      

  8.   

    谢谢,谢谢小新兄,jointan兄!~~