private void createimg()
{
this.pix1[renwuadd]=new PictureBox();
this.pix1[renwuadd].Location = new System.Drawing.Point(renwux,renwuy);
this.pix1[renwuadd].Name =renwuname;
this.pix1[renwuadd].Size = new System.Drawing.Size(176, 144);
this.pix1[renwuadd].Image=Image.FromFile(@"image\"+renwusuiji+".gif");
this.Controls.Add(pix1[renwuadd]);
}
我想动态的创建一个PictureBox在界面上。这个createimg()方法用来创建,而我又必须要在一个线程上执行这个方法。错误信息: 在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级。各位大虾,应该怎么才可以解决得到这个问题啊??????
谢谢。

解决方案 »

  1.   

    private void CreateImg()
    {
      if (this.InvokeRequired)
      {
        this.Invoke( new System.Threading.ThreadStart(this.CreateImgSync), null );
      }
      else
     {
        this.createimgsync();
      }
    }private void createimgsync()
    {
    this.pix1[renwuadd]=new PictureBox();
    this.pix1[renwuadd].Location = new System.Drawing.Point(renwux,renwuy);
    this.pix1[renwuadd].Name =renwuname;
    this.pix1[renwuadd].Size = new System.Drawing.Size(176, 144);
    this.pix1[renwuadd].Image=Image.FromFile(@"image\"+renwusuiji+".gif");
    this.Controls.Add(pix1[renwuadd]);
    }
      

  2.   

    参考这个帖子:
    http://community.csdn.net/Expert/topic/4455/4455126.xml
      

  3.   

    pix1[renwuadd].Left=44;
    pix1[renwuadd].Top=55;
    用Invoke的确可以了,但是我创建后,却无法在移动这个控件了。
      

  4.   

    把这个create方法包装在一个类中,把界面的form当作参数传给这个类,create方法就任意操作这个界面的form了。
      

  5.   

    这篇文章可能对你有所收获,
    通过多线程为基于 .NET 的应用程序实现响应迅速的用户
    http://www.microsoft.com/china/MSDN/library/enterprisedevelopment/softwaredev/misMultithreading.mspx