如题,程序界面不能动,没有响应。全部代码如下,请问大侠帮我看看哪里有问题
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.Net;
using System.Net.Sockets;
using System.Threading;namespace _232_TO_NET
{    public partial class Form1 : Form
    {
        private IPAddress IP;  
        private IPEndPoint MyServer;
        private Socket connectsock;
        public delegate void TextDelegate();
        public int IntTemp;
        public string HexString;
        public Form1()
        {
            InitializeComponent();
        }        private void btconnect_Click(object sender, EventArgs e)
        {
try
{
//把textBox1.Text输入的IP地址字符串转换为IPAddress格式;
IP=IPAddress.Parse (textIP.Text);
}
catch
{
MessageBox.Show ("您输入的IP地址格式不正确!");
}
try
{
MyServer=new IPEndPoint(IP,Int32.Parse (textPort.Text ));
connectsock=new Socket(AddressFamily.InterNetwork ,SocketType.Stream ,ProtocolType.Tcp );
connectsock.Connect (MyServer);
label1.Text="与主机"+textIP.Text +"端口"+textPort.Text+"连接成功!\r\n";               
}
catch(Exception ee)
{
                MessageBox.Show(ee.Message,"连接失败");
}
Thread thread1=new Thread (new ThreadStart (receive));
            thread1.Start ();

}
        private void receive()
{
            TextDelegate dd = new TextDelegate(this.receivefun);
            textReceive.BeginInvoke(dd);
            //Thread thread2 = new Thread(new ThreadStart(receive));
            //thread2.Start();
            //Thread.Sleep(10000);
}
        public void receivefun()
        {
            while (true)
            {
                Byte[] ReceiveBytes = new byte[32];
                NetworkStream netstream = new NetworkStream(connectsock);
                netstream.Read(ReceiveBytes, 0, ReceiveBytes.Length);//读取客户发送来的信息。
                if (checkBox1.Checked)
                {
                    foreach (char letter in ReceiveBytes)
                    {
                        if (letter != 0)
                        {
                            IntTemp = Convert.ToInt32(letter);
                            HexString = String.Format("{0:X}", IntTemp);
                            if (letter < 0x10)
                                textReceive.AppendText('0' + HexString + ' ');//写入到接收信息栏中。
                            else
                                textReceive.AppendText(HexString + ' ');//写入到接收信息栏中。
                        }
                    }
                    textReceive.AppendText("\n");
                }
                else
                {
                    string RecvMessage = System.Text.Encoding.ASCII.GetString(ReceiveBytes);
                    textReceive.AppendText(RecvMessage);//写入到接收信息栏中。
                }
            }
        }
        private void btsend_Click(object sender, EventArgs e)
        {
            try
{
Byte[] SendBytes=new Byte [64];
string SendString=textSend.Text;
//生成NetworkStream实例,用于发送基础数据流。
NetworkStream netstream=new NetworkStream (connectsock);
SendBytes=System.Text .Encoding .BigEndianUnicode .GetBytes(SendString.ToCharArray ());
netstream.Write (SendBytes,0,SendBytes.Length );//向socket服务器发送信息。
netstream.Flush ();
}
catch
{
MessageBox.Show ("连接没有建立!无法发送!");
}        }
    }
}

解决方案 »

  1.   

    textReceive.BeginInvoke(dd);
    又给UI线程阻塞了
    先看下别人如何写得,到网上找些例子看下
    www.codeproject.com上面有很多
      

  2.   

    搞定了,原因在于不能把 textReceive.BeginInvoke(dd);这一句放到代理的方法里面去放进去就阻塞。有同样问题的看看这代码吧,这个程序还没完成,改好的相对于之前发的代码改变的不多,对照看看吧,谢谢楼上帮助我的两位。
    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.Net;
    using System.Net.Sockets;
    using System.Threading;namespace _232_TO_NET
    {    public partial class Form1 : Form
        {
            private IPAddress IP;  
            private IPEndPoint MyServer;
            private Socket connectsock;
            public delegate void TextDelegate();
            public int IntTemp;
            public string HexString;
            public Byte[] ReceiveBytes = new byte[32];
            public Form1()
            {
                InitializeComponent();
            }        private void btconnect_Click(object sender, EventArgs e)
            {
    try
    {
    //把textBox1.Text输入的IP地址字符串转换为IPAddress格式;
    IP=IPAddress.Parse (textIP.Text);
    }
    catch
    {
    MessageBox.Show ("您输入的IP地址格式不正确!");
    }
    try
    {
    MyServer=new IPEndPoint(IP,Int32.Parse (textPort.Text ));
    connectsock=new Socket(AddressFamily.InterNetwork ,SocketType.Stream ,ProtocolType.Tcp );
    connectsock.Connect (MyServer);
    label1.Text="与主机"+textIP.Text +"端口"+textPort.Text+"连接成功!\r\n";               
    }
    catch(Exception ee)
    {
                    MessageBox.Show(ee.Message,"连接失败");
    }
    Thread thread1=new Thread (new ThreadStart (receive));
                thread1.Start ();

    }
            private void receive()
    {
                NetworkStream netstream = new NetworkStream(connectsock);
                netstream.Read(ReceiveBytes, 0, ReceiveBytes.Length);//读取客户发送来的信息。
                TextDelegate dd = new TextDelegate(this.receivefun);
                textReceive.BeginInvoke(dd);
                Thread thread2 = new Thread(new ThreadStart(receive));
                thread2.Start();
                //Thread.Sleep(10000);
    }
            public void receivefun()
            {                       if (checkBox1.Checked)
                {
                    foreach (char letter in ReceiveBytes)
                    {
                        if (letter != 0)
                        {
                            IntTemp = Convert.ToInt32(letter);
                            HexString = String.Format("{0:X}", IntTemp);
                            if (letter < 0x10)
                                textReceive.AppendText('0' + HexString + ' ');//写入到接收信息栏中。
                            else
                                textReceive.AppendText(HexString + ' ');//写入到接收信息栏中。
                        }
                    }
                    textReceive.AppendText("\n");
                }
                else
                {
                    string RecvMessage = System.Text.Encoding.ASCII.GetString(ReceiveBytes);
                    textReceive.AppendText(RecvMessage);//写入到接收信息栏中。
                }
                
            }
            private void btsend_Click(object sender, EventArgs e)
            {
                try
    {
    Byte[] SendBytes=new Byte [64];
    string SendString=textSend.Text;
    //生成NetworkStream实例,用于发送基础数据流。
    NetworkStream netstream=new NetworkStream (connectsock);
    SendBytes=System.Text .Encoding .BigEndianUnicode .GetBytes(SendString.ToCharArray ());
    netstream.Write (SendBytes,0,SendBytes.Length );//向socket服务器发送信息。
    netstream.Flush ();
    }
    catch
    {
    MessageBox.Show ("连接没有建立!无法发送!");
    }        }
        }
    }