2011-12-29 15 views
0
static string IP_Address = ""; 
getIPAddress(); 
MessageBox.Show(IP_Address); 

My機能は、このようなものです:一時停止プログラムの応答が得られるまで

public static void getIPAddress() 
{ 
    Uri uri = new Uri("http://whatismyip.org", UriKind.Absolute); 
    WebClient client = new WebClient(); 

    client.DownloadStringCompleted += (s, e) => 
     { 
      var res = e.Result; 
      IP_Address = res; 
     }; 
    client.DownloadStringAsync(uri); 
} 

私は私のクライアントのIPアドレスを設定するには、このコードを使用しています。しかし、問題は、コードを実行すると、最初は表示されているIP_Addressの空の文字列だけです

部分が実行されます。 IP_Addressが表示される前に、上記のコードブロックを実行するための提案。

答えて

1
getIPAddress((ip) => 
{ 
     MessageBox.Show(ip); 
}); 

public static void getIPAddress(Action<string> callback) 
{ 
     Uri uri = new Uri("http://whatismyip.org", UriKind.Absolute); 
     WebClient client = new WebClient(); 

     client.DownloadStringCompleted += (s, e) => 
     { 
      var res = e.Result; 

      callback(res); 
     }; 
     client.DownloadStringAsync(uri); 
} 
+0

ありがとうございます@ Ku6opr –

関連する問題