2013-02-21 74 views
0

我想使用来自客户端的IP地址连接到服务器。为了得到服务器的IP地址,我使用下面的代码,它正确地检测到服务器,但是我想在事件处理程序设置变量HostEndPoint之后执行一些其他功能,直到我需要等待,我该如何实现它? ..以下代码用于扫描是适当的方式?如果不是如何?如何在Windows Phone 7.1中实现等待功能

感谢

int startIp = 0; 
int EndIp = 255; 
int HostPort = 4678; 
string = ipPrefix = "192.168.1."; 
EndPoint HostEndPoint; 

private void ScanHosts() 
     { 

      Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 

     for (startIP = 0; startIP <= endIP; startIP++) 
     { 
      DnsEndPoint endPoint = new DnsEndPoint(ipPrefix + startIP.ToString(), HostPort); 

      try 
      { 
       SocketAsyncEventArgs socketEventArgs = new SocketAsyncEventArgs(); 
       socketEventArgs.RemoteEndPoint = endPoint; 
       socketEventArgs.UserToken = clientSocket; 
       socketEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(socketEventArgs_Connected); 


       clientSocket.ConnectAsync(socketEventArgs); 


      } 
      catch { } 

     } 

    } 

private void socketEventArgs_Connected(object sender, SocketAsyncEventArgs e) 
     { 
      if (e.SocketError == SocketError.Success) 
      { 
       HostEndPoint = e.RemoteEndPoint;     
      } 
     } 

回答

0

可以使用的AutoResetEvent它。 FE我的用法:

AutoResetEvent autoResetLoadDiscTitleAndPersonalDataById = new AutoResetEvent(false); 
            DataService.LoadDiscTitleAndPersonalDataById(titleToSync.id_on_server, titleInfoXML => 
            { 
             Title TitleInfo = new Title(titleInfoXML); 
             TitleInfo.UpdateToDb(); 
             autoResetLoadDiscTitleAndPersonalDataById.Set(); 
            }); 
            autoResetLoadDiscTitleAndPersonalDataById.WaitOne();