How to update a view without waiting for another operation to finish
lets say I have a GroupBox with several Labels. In these Labels, various
IP-related information are displayed. One info is the external IP address
of the machine.
string externalIP = "";
try
{
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
request.Timeout = 3000;
System.Threading.Tasks.Task<System.Net.WebResponse> response =
request.GetResponseAsync();
using (StreamReader stream = new
StreamReader(response.Result.GetResponseStream()))
{
if (response.Result.ContentLength != -1)
{
externalIP = stream.ReadToEnd();
}
}
}
catch (Exception e)
{
externalIP = "Error.";
}
if (externalIP == "")
{
return "No service.";
}
else
{
return externalIP = (new
Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")).Matches(externalIP)[0].ToString();
}
This method is called from following code:
private void updateNetworkIP()
{
string ip4e = "External IPv4: " + getExternalIPv4();
lblIP4external.Text = ip4e;
//Get some more info here.
}
How do I execute the code after getExternalIPv4() even when it's not
finished yet? It works when setting a TimeOut like I did above but
sometimes the request just takes a little longer but still completes
successfully. So I want to still be able to display the external IP but
continue to execute the other methods for refreshing the GroupBox.
No comments:
Post a Comment