【发布时间】:2009-05-12 13:23:51
【问题描述】:
我在c#中有两个线程。现在我需要等待一个特定的语句被执行,然后我才能在另一个线程中继续执行,这显然是一种同步的情况。 是否有任何代码可以像使用内置方法一样执行此操作?
这是代码示例:
public void StartAccept()
{
try
{
newSock.BeginAccept(new AsyncCallback(Accepted), newSock);
}
catch (ArgumentException)
{
MessageBox.Show("Error in arguments while using begin-accept", "Error", MessageBoxButtons.OK);
}
catch (ObjectDisposedException)
{
MessageBox.Show("socket closed while using begin-accept", "Error", MessageBoxButtons.OK);
}
catch (SocketException)
{
MessageBox.Show("Error accessing socket while using begin-accept", "Error", MessageBoxButtons.OK);
}
catch (InvalidOperationException)
{
MessageBox.Show("Invalid operation while using begin-accept", "Error", MessageBoxButtons.OK);
}
catch (Exception)
{
MessageBox.Show("Exception occurred while using begin-accept", "Error", MessageBoxButtons.OK);
}
}
这会从代码选择的所需主机接收数据:
private void listBox1_Click(object sender, EventArgs e)
{
String data = (String)this.listBox1.SelectedItem;
ip = Dns.GetHostAddresses(data);
clientIP = new IPEndPoint(ip[0], 5555);
newSock.Bind(clientIP);
newSock.Listen(100);
}
因此,为了开始接收数据,我需要将套接字初始化为特定的远程主机,这在我单击列表框中显示的主机之一时完成。 为此,我需要同步。
【问题讨论】:
-
抱歉,我错过了这个问题。
标签: c# multithreading synchronization