请问新建线程为何不能访问主窗口控件
想在新开线程中访问主窗口的控件,比如在文本框中写入当前线程工作的状态,但是VS2008提示不可,帮助信息实在太多了,没有看懂,请指教!下面是书上的例程:
private void button1_Click(object sender, EventArgs e)
{
try
{
myIP = IPAddress.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("IP地址格式错误,请重新输入!");
}
try
{
Thread thread = new Thread(new ThreadStart(accp));
thread.Start();
//accp();
}
catch (Exception ee)
{
textBox3.AppendText(ee.Message);
}
}
private void accp()
{
myServer = new IPEndPoint(myIP, Int32.Parse(textBox2.Text));
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Bind(myServer);
sock.Listen(50);
textBox3.AppendText("主机" + textBox1.Text + "端口" + textBox2.Text + "开始监听... ... \r\n"); //就是这句有问题啊!
accSock = sock.Accept();
if (accSock.Connected)
{
textBox3.AppendText("与客户建立联系");
while (check)
{
Byte[] Rec = new Byte[64];
NetworkStream netStream = new NetworkStream(accSock);
netStream.Read(Rec, 0, Rec.Length);
string RecMessage = System.Text.Encoding.BigEndianUnicode.GetString(Rec);
richTextBox1.AppendText(RecMessage + "\r\n");
}
}
}
居然不能执行,晕死啊!