public
Last active

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
void ShowStuff ()
{
var client = new WebClient ();
var content = JsonValue.Parse (client.DownloadString ("http://api.worldbank.org/countries?format=json&per_page=50"));
int number_of_countries = content [0] ["total"];
int done = 0, error = 0;
 
InvokeOnMainThread (() => {
CountriesLabel.Text = string.Format ("Countries: {0} done: 0 error: 0", number_of_countries);
});
 
foreach (JsonObject c in content [1]) {
string country_url = string.Format ("http://api.worldbank.org/countries/{0}/indicators/NY.GDP.MKTP.CD&format=json", (string)c ["id"]);
JsonValue json = null;
 
try {
json = JsonValue.Parse (client.DownloadString (country_url));
} catch (Exception e){
++error;
InvokeOnMainThread (()=> status.Text = "Got exception "+ e);
continue;
}
 
ThreadPool.QueueUserWorkItem (delegate {
Map map = null;
try {
map = LoadCountryLogo (c ["name"]).Result;
} catch (Exception e){
++error;
InvokeOnMainThread (()=> status.Text = "Got exception "+ e);
}
if (map != null){
ThreadPool.QueueUserWorkItem (delegate {
Position position = null;
 
try {
position = LookupCountryPosition (c ["longitude"], c ["latitude"]).Result;
if (position != null)
InvokeOnMainThread (() => {
AddPin (map, position);
++done;
status.Text = json ["name"];
 
});
} catch (Exception e){
error++;
InvokeOnMainThread (()=> status.Text = "Got exception "+ e);
}
 
});
}
});
InvokeOnMainThread (() => CountriesLabel.Text = string.Format ("Countries: {0} done: {1} error: {2}", number_of_countries, done, error));
}
InvokeOnMainThread (() => {
CountriesLabel.Text = string.Format ("Countries: {0}", number_of_countries);
});
}

I don't want to imagine how this thing would look like if you had implemented it properly with callbacks. :O

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.