How do I create a background thread
private class myTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { } @Override protected Void doInBackground(Void... params) { // Runs on the background thread return null; } @Override protected void onPostExecute(Void res) { } }
and to run it
new myTask().execute();
If the .Net/Mono version you're using supports async/await then you can simply do
async void DisplayNews(){ string url = "http://rapstation.com/webservice.php"; HttpClient client = new HttpClient(); string content = await client.GetStringAsync(url); NewsObject[] news = JsonConvert.DeserializeObject<NewsObject[]>(content); //!! Your code to add news to some control}
if Not, then you can use Task's
void DisplayNews2(){ string url = "http://rapstation.com/webservice.php"; Task.Factory.StartNew(() => { using (var client = new WebClient()) { string content = client.DownloadString(url); return JsonConvert.DeserializeObject<NewsObject[]>(content); } }) .ContinueWith((task,y) => { NewsObject[] news = task.Result; //!! Your code to add news to some control },null,TaskScheduler.FromCurrentSynchronizationContext());}