标题:请问新建线程为何不能访问主窗口控件
取消只看楼主
wuyanmin
Rank: 1
等 级:新手上路
帖 子:10
专家分:1
注 册:2010-5-29
结帖率:50%
已结贴  问题点数:10 回复次数:4 
请问新建线程为何不能访问主窗口控件
想在新开线程中访问主窗口的控件,比如在文本框中写入当前线程工作的状态,但是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");
                }
            }
        }

居然不能执行,晕死啊!
搜索更多相关主题的帖子: IP地址 文本框 
2011-04-16 15:43
wuyanmin
Rank: 1
等 级:新手上路
帖 子:10
专家分:1
注 册:2010-5-29
得分:0 
那该怎么办呢?这个是书上的原代码啊!很郁闷……
2011-04-16 20:15
wuyanmin
Rank: 1
等 级:新手上路
帖 子:10
专家分:1
注 册:2010-5-29
得分:0 
这条语句加在何处呢?提供例程的书是2002年出版的,那个时候可以执行上述代码而没有限制么?
2011-04-18 23:02
wuyanmin
Rank: 1
等 级:新手上路
帖 子:10
专家分:1
注 册:2010-5-29
得分:0 
改成这样成么?

        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);
            }
        }

        public delegate void MyInvoke();
        
        private void accp()
        {
            MyInvoke mi = new MyInvoke(showServerMessage);
            BeginInvoke(mi);
        }

        public void showServerMessage()
        {
            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");
                }
            }
        }

还是有错误啊!
2011-04-19 00:29
wuyanmin
Rank: 1
等 级:新手上路
帖 子:10
专家分:1
注 册:2010-5-29
得分:0 
看了一些资料,现在错误更加多了,5555555555……
2011-04-28 21:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-336903-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.059741 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved