I get crashes when I work with my app with a slow internet when I keep pressing different buttons that gathers data via httprequests. how could i make a function so that if i do not recieve the data within for example 10 seconds, the attempt to gather the data from the httprequest should be cancelled.
This is my code:
static public async Task<JObject> getCategories ()
{
var httpClientRequest = new HttpClient ();
try {
var result = await httpClientRequest.GetAsync ("http://localhost");
var resultString = await result.Content.ReadAsStringAsync ();
var jsonResult = JObject.Parse (resultString);
System.Diagnostics.Debug.WriteLine (resultString);
return jsonResult;
}
catch {
return null;
}
}
async void createCategory (object sender, EventArgs args)
{
var getCategory = await parseAPI.getCategories ();
if (getCategory != null) {
foreach (var currentItem in getItems["results"]) {
id = currentItem ["ID"].ToString ();
objectid = currentItem ["objectid"].ToString ();
//connected
}
}
} else {
//no connection
}
}