如何在Windows Phone 7.1中实现等待功能

时间:2013-02-21 05:45:23

标签: c# silverlight windows-phone-7 windows-phone-7.1

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

谢谢

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;                
            }
        }

1 个答案:

答案 0 :(得分: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();