I am trying to create a little application and display images of cats from thecatapi.com. I call CATS() on a timer every 10 seconds. The code runs fine for 3-5 images then i get "System.ArgumentException: 'parameter is not valid.'" Ive tried to use try/catch but it still throws an exception. Pointers as to a solution are much appreciated.
string catURL;
string catJSON;
private async void CATS()
{
var client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("https://api.thecatapi.com/v1/images/search?api_key=<apiKey>");
HttpContent responseContent = response.Content;
using(var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
catJSON = (await reader.ReadToEndAsync());
}
dynamic items = JsonConvert.DeserializeObject(catJSON);
foreach(var item in items)
{
catURL = Convert.ToString(item.url);
}
try
{
if (pictureBox1.Image != null)
pictureBox1.Image.Dispose();
pictureBox1.Load(catURL);
}
catch {}
}