在线程里对windows的窗口修改 是怎么实现的啊?是不是不能在同有一 个类里面调用呢? 比如 我要在一个线程的方法里 对textbox.text附值 要怎么做啊?象下面的程序 运行到 richTextBox1.Text = str1 时,编译器就说:"线程间操作无效:,从不是创建控件“richTextBox1”的线程访问它" 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 static string str1; public static string str2; bool b1 = false; bool b2 = false; public Form1() { InitializeComponent(); }
private void Form1_dgCount1() { richTextBox1.Text = str1; }
private void Form1_dgCount2() { richTextBox2.Text = str2; } public void WorkerThread1() { Thread.CurrentThread.Name = "11"; //MessageBox.Show(Thread.CurrentThread.Name); //Form1 myform = new Form1(); for (int i = 1; i <= 100; i += 2) { str1 = str1 + i.ToString() + " "; // myform.Form1_dgCount1(); this.Form1_dgCount1(); System.Threading.Thread.Sleep(10);
} this.b1 = true; if (b2) { this.Form1_BackColorChanged(this, null); } Thread.CurrentThread.Abort();
}
public void WorkerThread2() { //Form1 myform2 = new Form1(); Thread.CurrentThread.Name = "12"; //MessageBox.Show(Thread.CurrentThread.Name); for (int j = 2; j <= 100; j += 2) { str2 = str2 + j.ToString() + " "; // myform2.Form1_dgCount2(); this.Form1_dgCount2(); System.Threading.Thread.Sleep(10);
} this.b2 = true; if (b1) { this.Form1_BackColorChanged(this, null); } Thread.CurrentThread.Abort();
} private void Form1_BackColorChanged(object sender, System.EventArgs e) { this.BackColor = Color.Red; }
private void button1_Click(object sender, EventArgs e) { ThreadStart worker1 = new ThreadStart(WorkerThread1); ThreadStart worker2 = new ThreadStart(WorkerThread2);
Thread t1 = new Thread(worker1); Thread t2 = new Thread(worker2);
t1.Start(); t2.Start(); } } }