private static ManualResetEvent connectDone = new ManualResetEvent(false);private void button1_Click(object sender, System.EventArgs e)
{
Socket s = new Socket AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress ipaddr = IPAddress.Parse("127.0.0.1");
IPEndPoint ipe = new IPEndPoint(ipaddr,8900); s.BeginConnect(ipe,new AsyncCallback(ConnectCallback),s);
connectDone.WaitOne();
}private static void ConnectCallback(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
if (client.Connected == false)
{
IPAddress ipaddr = IPAddress.Parse("127.0.0.1");
IPEndPoint ipe = new IPEndPoint(ipaddr,8900);
client.BeginConnect(ipe,new AsyncCallback(ConnectCallback),client);
}
else
{
client.EndConnect(ar);
connectDone.Set();
}
}
1。为何第一行的ManualResetEvent类型定义前要加static关键字?
2。为何后面的ConnectCallback函数的定义前面也要加static关键字?
多谢。
{
Socket s = new Socket AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress ipaddr = IPAddress.Parse("127.0.0.1");
IPEndPoint ipe = new IPEndPoint(ipaddr,8900); s.BeginConnect(ipe,new AsyncCallback(ConnectCallback),s);
connectDone.WaitOne();
}private static void ConnectCallback(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
if (client.Connected == false)
{
IPAddress ipaddr = IPAddress.Parse("127.0.0.1");
IPEndPoint ipe = new IPEndPoint(ipaddr,8900);
client.BeginConnect(ipe,new AsyncCallback(ConnectCallback),client);
}
else
{
client.EndConnect(ar);
connectDone.Set();
}
}
1。为何第一行的ManualResetEvent类型定义前要加static关键字?
2。为何后面的ConnectCallback函数的定义前面也要加static关键字?
多谢。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货