线程间直接操作是无效,可以用一个代理
主线程:
第一步:定义委托delegate
private delegate void setConn(string strConn);
private setConn mySetConn = null;
第二步:定义一个方法
private void DispText(string strConn)
{
    textBox1.Text = strConn;
}
第三步:初始化委托
mySetConn = new setConn(this.DispText);
第四步:在工作线程中
this.Invoke(mySetConn, "把我显示在textBox1中,谢谢!");

解决方案 »

  1.   

    private delegate void dodelegate(System.Windows.Forms.ProgressBar pb);
            dodelegate mydo = null;
            private void doit(System.Windows.Forms.ProgressBar pb)
            {
                if (progressBar1.InvokeRequired)
                {
                    pb.Minimum = 0;
                    pb.Maximum = 100;
                    pb.Step = 1;
                    pb.Value = 0;
                    for (int i = 0; i < 100; i++)
                    {
                        pb.Value = pb.Value + 1;
                        System.Threading.Thread.Sleep(200);
                    }
                }
            }
            private void button4_Click(object sender, EventArgs e)
            {
                mydo = new dodelegate(doit);
                mydo.BeginInvoke(progressBar1,null,null);
            }
    progressbar1为一个窗体上的控件,但是在执行时,仍然报线程间操作无效: 从不是创建控件“progressBar1”的线程访问它,有高手能解答吗,谢谢!最好能贴出代码片断
      

  2.   

    线程中不能直接给控件赋值,需要调用控件的Invoke方法
      

  3.   

    先看看这篇文章,我回答部分
    http://community.csdn.net/Expert/topic/5339/5339904.xml?temp=.8979608
      

  4.   

    public partial class Form1 : Form
        {
            private delegate void dodelegate(System.Windows.Forms.ProgressBar pb);
            public Form1()
            {
                InitializeComponent();
            }        private void button4_Click(object sender, EventArgs e)
            {
                dodelegate mydo = new dodelegate(doit);
                this.Invoke(mydo, new object[] { progressBar1 });
            }        private void doit(System.Windows.Forms.ProgressBar pb)
            {
                pb.Minimum = 0;
                pb.Maximum = 100;
                pb.Step = 1;
                pb.Value = 0;
                for (int i = 0; i < 100; i++)
                {
                    pb.Value = pb.Value + 1;
                    System.Threading.Thread.Sleep(200);
                }
            }
        }
    不过这样写窗体会假死
      

  5.   

    用下面的方法就不会发生假死了
    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 WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button4_Click(object sender, EventArgs e)
            {
                Worker worker = new Worker();
                worker.ProcessEvent += new ProcessEventHandler(worker_ProcessEvent);
                worker.Start();
            }        private void worker_ProcessEvent(int count)
            {
                this.Invoke(new ProcessEventHandler(this.ShowProcess), new object[] { count });
            }        private void ShowProcess(int count)
            {
                progressBar1.Value = count;
            }
        }    public delegate void ProcessEventHandler(int count);
        public class Worker
        {
            public event ProcessEventHandler ProcessEvent = null;        public void Start()
            {
                Thread thread = new Thread(new ThreadStart(delegate()
                {
                    for (int i = 0; i < 100; i++)
                    {
                        if (ProcessEvent != null)
                        {
                            ProcessEvent(i);
                        }
                        Thread.Sleep(200);
                    }            
                }));
                thread.IsBackground = true;
                thread.Start();
            }
        }
    }