初学,用vs2005新建了个c#的window应用程序,上面就3个textbox和1个button,输入文本控件分别是ip地址,端口,信息输出。按钮是“开始监听”,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace tcpip
{
public partial class Form1 : Form
{
private IPAddress myIP = IPAddress.Parse("192.168.1.29");
private IPEndPoint myServer;
private Socket sock;
private bool check=true;
private Socket accSock; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
} 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();
}
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);
//下面这句出错,标题是:未处理InvalidOperationException。
//错误信息是:"线程间操作无效: 从不是创建控件“textBox3”的线程访问它。"
//textbox3是打算用来输出一些信息的控件
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"); }
}
}
}
}实在是初学到不能再初了,一点基础都没有,不知道这个问题应该如何解决,请各位热心人帮忙指点下把。谢谢了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace tcpip
{
public partial class Form1 : Form
{
private IPAddress myIP = IPAddress.Parse("192.168.1.29");
private IPEndPoint myServer;
private Socket sock;
private bool check=true;
private Socket accSock; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
} 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();
}
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);
//下面这句出错,标题是:未处理InvalidOperationException。
//错误信息是:"线程间操作无效: 从不是创建控件“textBox3”的线程访问它。"
//textbox3是打算用来输出一些信息的控件
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"); }
}
}
}
}实在是初学到不能再初了,一点基础都没有,不知道这个问题应该如何解决,请各位热心人帮忙指点下把。谢谢了
{
textBox3.Invoke((MethodInvoker)delegate()
{
textBox3.AppendText("主机:"+textBox1.Text+"端口:"+textBox2.Text+"开始监听......\r\n");
});
}
else
{
textBox3.AppendText("主机:"+textBox1.Text+"端口:"+textBox2.Text+"开始监听......\r\n");
}试试看吧
同意。非主线程中访问winform控件,必须记得有一行 InvokeRequired 代码。当然,可以用扩展方法技术包装一下,使得语法更简练。