参考如下,主要是对InvokeRequired的应用,下面的代码在线程中显示时间:private void Form1_Load(object sender, EventArgs e){ Thread th = new Thread(new ParameterizedThreadStart(this.thproc)); th.Start(this);}private void thproc(object obj){ Form1 owner = obj as Form1; if (owner == null) { return; } while (true) { Thread.Sleep(1000); owner.SetText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); }}delegate void MethodInvoker(string text);private void SetText(string p){ if (this.InvokeRequired) { MethodInvoker invoker=new MethodInvoker(this.SetText); this.Invoke(invoker, p); return; } this.textBox1.Text = p;} 追问 Form1 owner = obj as Form1;抛出异常 未将对象引用设置到对象的实例。 追答 你换成你自己的类型啊,这个只是一个思路。 追问 我换了 还是异常
winfrom中,默认不允许跨线程修改控件值,这样做有对安全和性能很差,推荐用 BeginInvoke代理调用, 参考,http://www.csharpwin.com/csharpspace/12024r3622.shtml如果嫌麻烦,可以用匿名委托和linq的lambda表达式