private Thread Main_Thread;
        private void frm_main_Load(object sender, EventArgs e)
        {
            ThreadStart TS = new ThreadStart(Create);
            Main_Thread = new Thread(TS);
            Main_Thread.Start();
        }
        private void Create()
        {
            PictureBox p = new PictureBox();
            p.Name = "p" + i.ToString();
            p.ImageLocation = @"C:\Documents and Settings\Administrator\桌面\素材\房子\Home5.png";
            p.Size = new System.Drawing.Size(60, 60);
            p.Location = new System.Drawing.Point(30, 50);
            p.Cursor = System.Windows.Forms.Cursors.Hand;
            this.Panel2.Controls.Add(p);--这里报错:线程间操作无效 从不是创建控件“”的线程访问它        }Create()方法里面有个for循环 如果加到了Load里面 生成的PictureBox多了主界面就会假死 等生成完了才没事
就想用个线程来处理 加上线程了就报错了:线程间操作无效 从不是创建控件“”的线程访问它
程序运行了以后会有个进度条显示生成的进度  这个线程硬挨怎么写呢

解决方案 »

  1.   

    加上线程是不能调试的,
    你试试
    Ctrl+F5,这个是直接编译运行
      

  2.   

    在MSDN上看到:使用多线程提高 Windows 窗体应用程序的性能时,必须注意以线程安全方式调用控件。
    .NET Framework 有助于在以非线程安全方式访问控件时检测到这一问题。在调试器中运行应用程序时,如果创建某控件的线程之外的其他线程试图调用该控件,则调试器会引发一个 InvalidOperationException,并提示消息:“从不是创建控件 control name 的线程访问它。” 这个该怎么解决啊  给个思路吧 有个小例子也好
      

  3.   

    this.Panel2.Controls.Add(p);->this.Invoke((EventHandler)delegate{this.Panel2.Controls.Add(p);});
      

  4.   

    楼上正解  谢谢了 增加完了 以后  Panel2有滚动条了 拖动滚动条的时候 panel2里面就全是那张图片了  花了
    这个是为什么啊
      

  5.   

    public partial class Form1 : Form
        {
            public delegate void myDelete();//声明一个委托        public Form1()
            {
                InitializeComponent();           
            }        public void Create()
            {
                PictureBox p = new PictureBox();
                p.Name = "myPic";
                p.ImageLocation = @"C:\Documents and Settings\Administrator\桌面\素材\房子\Home5.png";
                p.Size = new System.Drawing.Size(60, 60);
                p.Location = new System.Drawing.Point(30, 50);
                p.Cursor = System.Windows.Forms.Cursors.Hand;            panel1.Controls.Add(p);
            }        public void newThread()
            {
                System.Threading.Thread.Sleep(2000);//线程休眠2秒
                panel1.BeginInvoke(new myDelete(Create));//在button1所在线程上执行“设置文字”
            }        private void Form1_Load(object sender, EventArgs e)
            {
                new System.Threading.Thread(new System.Threading.ThreadStart(newThread)).Start();//创建一个新的线程并启动
            }
        }
    测试完毕,无错
      

  6.   

    public partial class Form1 : Form
        {
            public delegate void myDelete();//声明一个委托        public Form1()
            {
                InitializeComponent();           
            }        public void Create()
            {
                PictureBox p = new PictureBox();
                p.Name = "myPic";
                p.ImageLocation = @"C:\Documents and Settings\Administrator\桌面\素材\房子\Home5.png";
                p.Size = new System.Drawing.Size(60, 60);
                p.Location = new System.Drawing.Point(30, 50);
                p.Cursor = System.Windows.Forms.Cursors.Hand;            panel1.Controls.Add(p);
            }        public void newThread()
            {
                System.Threading.Thread.Sleep(2000);//线程休眠2秒
                panel1.BeginInvoke(new myDelete(Create));//在button1所在线程上执行“设置文字”
            }        private void Form1_Load(object sender, EventArgs e)
            {
                new System.Threading.Thread(new System.Threading.ThreadStart(newThread)).Start();//创建一个新的线程并启动
            }
        }
    测试完毕,无错