我该如何是好,请大仙们帮帮我~~!

笑羽酣天 2008-11-26 05:07:16
我想做个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();
}

}
}
...全文
191 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
soaringbird 2008-11-27
  • 打赏
  • 举报
回复
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();//需要在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中
}

}
}
abcyzq 2008-11-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 heyu1000 的回复:]
BeginListen方法是不是另外开了一个线程,如果是这样的话,看看下面的代码是否对你有用
if (this.InvokeRequired)
{
this.Invoke(new TestDelegate(TestEventHandler), new object[] { obj});
return;
}
这个代码只是参照,你直接用肯定不行,你可以查一下InvokeRequired以及Invoke的用法
[/Quote]

与此楼的方法的方法差不多,楼主去查下Invoke,InvokeRequired相关资料。
nnoovvee 2008-11-27
  • 打赏
  • 举报
回复
up
笑羽酣天 2008-11-27
  • 打赏
  • 举报
回复
我试 了几个但都不成功,能給点详细的代码吗?
bloodish 2008-11-27
  • 打赏
  • 举报
回复
AsyncOperation asyncOperation = AsyncOperationManager.CreateOperation(null);
asyncOperation.Post
{
delegate
{BeginListen();},null
};
笑羽酣天 2008-11-27
  • 打赏
  • 举报
回复
谁在来出个注意
soaringbird 2008-11-27
  • 打赏
  • 举报
回复
using System;
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});
}
}
}
heyu1000 2008-11-26
  • 打赏
  • 举报
回复
BeginListen方法是不是另外开了一个线程,如果是这样的话,看看下面的代码是否对你有用
if (this.InvokeRequired)
{
this.Invoke(new TestDelegate(TestEventHandler), new object[] { obj});
return;
}
这个代码只是参照,你直接用肯定不行,你可以查一下InvokeRequired以及Invoke的用法
soaringbird 2008-11-26
  • 打赏
  • 举报
回复
// The following code assumes a 'ListBox' and a 'Button' control are added to a form,
// 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);
}
}


soaringbird 2008-11-26
  • 打赏
  • 举报
回复
執行緒?线程吧?
界面控件是不能直接跨线程访问的,得用委托

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧