- C# code
MDI父using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WhyThread{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Form2 f = new Form2(this); f.Show(); } }}子窗口using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading;namespace WhyThread{ public partial class Form2 : Form { Thread[] threads; public Form2(Form1 parent) { InitializeComponent(); MdiParent = parent; } private void Form2_Load(object sender, EventArgs e) { } void Do() { richTextBox1.Text += DateTime.Now.ToString(); } void ThreadProc(Object obj) { (obj as Form2).Do(); } private void button1_Click(object sender, EventArgs e) { threads = new Thread[100]; for (int i = 0; i < threads.Length; i++) { threads[i] = new Thread(ThreadProc); threads[i].IsBackground = true; threads[i].Start(this); } } }}
------解决方案--------------------
不是。。
richTextBox1.Invoke(()=>
{
richTextBox1.Text += DateTime.Now.ToString();
});
------解决方案--------------------
winform只允许控件所在的线程,给控件进行赋值,在其它线程需要用delegate.
参考:
winform线程给lable赋值
http://www.cnblogs.com/zhang9418hn/archive/2011/11/05/2237105.html
winform中Control.Invoke方法返回窗体主线程给控件赋值
http://www.cnblogs.com/xyz168/archive/2011/08/08/2130946.html