我想做个socket来测试测试,结果服务器端老是出错:说是“跨執行緒作業無效: 存取控制項 'listBox1' 時所使用的執行緒與建立控制項的執行緒不同。”
出错的地方是“label1.Text = iep.ToString();”和“listBox1.Items.Add(msg);”。
我该如何是好,请大仙们帮帮我~~!
以下是代码
private void BeginListen()
{
IPAddress ServerIP = GetServerIP();
IPEndPoint iep = new IPEndPoint(ServerIP, 8000);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] byteMessage = new byte[100];
label1.Text = iep.ToString();
socket.Bind(iep);
while (true)
{
try
{
socket.Listen(5);
Socket newSocket = socket.Accept();
newSocket.Receive(byteMessage); string sTime = DateTime.Now.ToShortTimeString();
string msg = sTime + ":Message from:";
msg += newSocket.RemoteEndPoint.ToString() + Encoding.Default.GetString(byteMessage);
listBox1.Items.Add(msg);
}
catch (SocketException ex)
{
label1.Text += ex.ToString();
}
}
}
出错的地方是“label1.Text = iep.ToString();”和“listBox1.Items.Add(msg);”。
我该如何是好,请大仙们帮帮我~~!
以下是代码
private void BeginListen()
{
IPAddress ServerIP = GetServerIP();
IPEndPoint iep = new IPEndPoint(ServerIP, 8000);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] byteMessage = new byte[100];
label1.Text = iep.ToString();
socket.Bind(iep);
while (true)
{
try
{
socket.Listen(5);
Socket newSocket = socket.Accept();
newSocket.Receive(byteMessage); string sTime = DateTime.Now.ToShortTimeString();
string msg = sTime + ":Message from:";
msg += newSocket.RemoteEndPoint.ToString() + Encoding.Default.GetString(byteMessage);
listBox1.Items.Add(msg);
}
catch (SocketException ex)
{
label1.Text += ex.ToString();
}
}
}
解决方案 »
- 去掉DevExpress gridControl控件表头上的的默认英文字母
- 该行不属于与此关系相同的数据集
- 求先入先出算法
- 如何将C#编译的ASPX文件生成Html静态页面?
- 将dataset写回数据库,不用适配器如何做呀?
- (菜鸟问题)怎么设置快捷键
- 如何提取中间字符串
- 在开发个小型进销存,不用服务器端直接客户端远程连接数据库行不行,后期会有什么问题吗?
- [300分]大家帮帮忙,困扰我很久的问题了,关于动态生成控件的问题!!!
- 请问 怎样在c#中使用数据库制作大学排课软件?
- 有什么方法可以查看当一个软件运行是注册了那些控件(库)或使用了那些控件(库)?
- this.refresh() 和 Application.Doevents()区别
// containing a delegate which encapsulates a method that adds items to the listbox. public class MyThreadClass
{
MyFormControl myFormControl1;
public MyThreadClass(MyFormControl myForm)
{
myFormControl1 = myForm;
} public void Run()
{
// Execute the specified delegate on the thread that owns
// 'myFormControl1' control's underlying window handle.
myFormControl1.Invoke(myFormControl1.myDelegate);
}
}
if (this.InvokeRequired)
{
this.Invoke(new TestDelegate(TestEventHandler), new object[] { obj});
return;
}
这个代码只是参照,你直接用肯定不行,你可以查一下InvokeRequired以及Invoke的用法
与此楼的方法的方法差不多,楼主去查下Invoke,InvokeRequired相关资料。
{
IPAddress ServerIP = GetServerIP();
IPEndPoint iep = new IPEndPoint(ServerIP, 8000);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] byteMessage = new byte[100];
label1.Text = iep.ToString();//需要在invoke中
socket.Bind(iep);
while (true)
{
try
{
socket.Listen(5);
Socket newSocket = socket.Accept();
newSocket.Receive(byteMessage); string sTime = DateTime.Now.ToShortTimeString();
string msg = sTime + ":Message from:";
msg += newSocket.RemoteEndPoint.ToString() + Encoding.Default.GetString(byteMessage);
listBox1.Items.Add(msg); //需要在invoke中
}
catch (SocketException ex)
{
label1.Text += ex.ToString(); //需要在invoke中
}
}
}
using System.Drawing;
using System.Windows.Forms;
using System.Threading; public class MyFormControl : Form
{
public delegate void AddListItem(String myString);
public AddListItem myDelegate;
private Button myButton;
private Thread myThread;
private ListBox myListBox;
public MyFormControl()
{
myButton = new Button();
myListBox = new ListBox();
myButton.Location = new Point(72, 160);
myButton.Size = new Size(152, 32);
myButton.TabIndex = 1;
myButton.Text = "Add items in list box";
myButton.Click += new EventHandler(Button_Click);
myListBox.Location = new Point(48, 32);
myListBox.Name = "myListBox";
myListBox.Size = new Size(200, 95);
myListBox.TabIndex = 2;
ClientSize = new Size(292, 273);
Controls.AddRange(new Control[] {myListBox,myButton});
Text = " 'Control_Invoke' example ";
myDelegate = new AddListItem(AddListItemMethod);
}
static void Main()
{
MyFormControl myForm = new MyFormControl();
myForm.ShowDialog();
}
public void AddListItemMethod(String myString)
{
myListBox.Items.Add(myString);
}
private void Button_Click(object sender, EventArgs e)
{
myThread = new Thread(new ThreadStart(ThreadFunction));
myThread.Start();
}
private void ThreadFunction()
{
MyThreadClass myThreadClassObject = new MyThreadClass(this);
myThreadClassObject.Run();
}
}
public class MyThreadClass
{
MyFormControl myFormControl1;
public MyThreadClass(MyFormControl myForm)
{
myFormControl1 = myForm;
}
String myString; public void Run()
{
for (int i = 1; i <= 5; i++)
{
myString = "Step number " + i.ToString() + " executed";
Thread.Sleep(400);
// Execute the specified delegate on the thread that owns
// 'myFormControl1' control's underlying window handle with
// the specified list of arguments.
myFormControl1.Invoke(myFormControl1.myDelegate,
new Object[] {myString});
}
}
}
asyncOperation.Post
{
delegate
{BeginListen();},null
};