首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#线程内访问线程外数据解决方案

2012-12-18 
C#线程内访问线程外数据是一个winform小程序。主要功能是:1、在窗体运行前新建线程,在线程内开启socket服务。

C#线程内访问线程外数据
是一个winform小程序。主要功能是:

1、在窗体运行前新建线程,在线程内开启socket服务。
2、当有新的连接时,在主窗口的一个textbox中打印相关信息。

主要问题:在线程内的数据无法在textbox中打印出来。

提示:“线程间操作无效: 从不是创建控件“textBox1”的线程访问它”

源代码:

namespace Server_2
{
    public partial class Form1 : Form
    {
        Thread aThread = null;
        private delegate void FlushClient(string s); //代理
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text += ("正在开启server服务" + "\n" + ".......请稍候..." + "\n");
            aThread = new Thread(new ThreadStart(StartSocketServer));
            aThread.IsBackground = true;
            aThread.Start();
            textBox1.Text += ("server is start!" + "\n");
        }
        public void StartSocketServer()
        {

            IPAddress ip = IPAddress.Parse("192.168.168.163");
            //IP地址跟端口的组合
            IPEndPoint iep = new IPEndPoint(ip, 8001);
            //创建Socket
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //绑定Socket
            socket.Bind(iep);
            //服务器已经做好接收任何连接的准备
            socket.Listen(10);
            while (true)
            {
                //执行accept方法
                Socket Client = socket.Accept();
                byte[] message = new byte[1024];
                NetworkStream networkStream = new NetworkStream(Client);
                int len = networkStream.Read(message, 0, message.Length);
                //byte数组转换成string


                string output = System.Text.Encoding.Unicode.GetString(message);
                string pagehtml = ("一共从客户端接收了" + len.ToString() + "字节。接收字符串为:" + output);
                ThreadFunction(pagehtml);
            }
        }
        private void ThreadFunction(string s)
        {
                textBox1.Text = s;
        }
    }
}





我曾想过用委托。但是初学这玩意,有些地方没想明白。望高手指点。
[最优解释]
1、简单点可以使用:CheckForIllegalCrossThreadCalls
2、使用BeginInvoke, 也就是InvokeRequired这种形式。

[其他解释]
在启动线程之前设置CheckForIllegalCrossThreadCalls=false
[其他解释]
要用委托http://www.cnblogs.com/mokey/articles/2095457.html
[其他解释]
delegate void printhehe(string s)

?private?void?ThreadFunction(string?s)
????????{
????????????if?(this.textBox1.InvokeRequired)//等待异步
????????????{
???????????????
?????????????????this.Invoke(printhehe(ThreadFunction),new object[]{s});?
?????????????
????????????}
????????????else
????????????{
????????????????this.textBox1.Text?=?s;//<span style="color: #FF0000;">这里</span>
????????????}
????????}
[其他解释]
委托或者允许线程不安全访问
[其他解释]
backgroundworker 满足你的需求
[其他解释]
引用:
要用委托http://www.cnblogs.com/mokey/articles/2095457.html


我现在该了点,但是还是没搞懂下面这几行怎么写。。。。。

namespace Server_2
{
    public partial class Form1 : Form
    {
        Thread aThread = null;
        private delegate void FlushClient(string s); //代理
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text += ("正在开启server服务" + "\n" + ".......请稍候..." + "\n");
            aThread = new Thread(new ThreadStart(StartSocketServer));
            aThread.IsBackground = true;


            aThread.Start();
            textBox1.Text += ("server is start!" + "\n");
        }
        public void StartSocketServer()
        {

            IPAddress ip = IPAddress.Parse("192.168.168.163");
            //IP地址跟端口的组合
            IPEndPoint iep = new IPEndPoint(ip, 8001);
            //创建Socket
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //绑定Socket
            socket.Bind(iep);
            //服务器已经做好接收任何连接的准备
            socket.Listen(10);
            while (true)
            {
                //执行accept方法
                Socket Client = socket.Accept();
                byte[] message = new byte[1024];
                NetworkStream networkStream = new NetworkStream(Client);
                int len = networkStream.Read(message, 0, message.Length);
                //byte数组转换成string
                string output = System.Text.Encoding.Unicode.GetString(message);
                string pagehtml = ("一共从客户端接收了" + len.ToString() + "字节。接收字符串为:" + output);
                ThreadFunction(pagehtml);
            }
        }
        private void ThreadFunction(string s)
        {
            if (this.textBox1.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(ThreadFunction);
                 this.Invoke(fc); //通过代理调用刷新方法
                 this.textBox1.Text = s;//这里
            }


            else
            {
                this.textBox1.Text = s;//这里
            }
        }
    }
}


[其他解释]
引用:
delegate void printhehe(string s)

 private void ThreadFunction(string s)
        {
            if (this.textBox1.InvokeRequired)//等待异步
            {
               
              ……


  this.Invoke(printhehe(ThreadFunction),new object[]{s}); //printhehe?????


[其他解释]
求,简单例子!
[其他解释]
引用:
在启动线程之前设置CheckForIllegalCrossThreadCalls=false


CheckForIllegalCrossThreadCalls=false这玩意搞定。

但是没搞懂是啥子意思。。。。

热点排行