Skip to content

Instantly share code, notes, and snippets.

@kumpera
Last active December 14, 2015 19:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kumpera/5137005 to your computer and use it in GitHub Desktop.
Save kumpera/5137005 to your computer and use it in GitHub Desktop.
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);
});
}
@tamasflamich
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment