I have a UserControl
, which I use to send a HTTP request to an API. The code for sending, is located in the ViewModel
, but it is invoked on by the UserControl, currently using the Load
event. While awaiting completion of the HTTP request, I would like to show an animated gif.
The problem is this: I can succesfully show the gif, using the load event and make it go away, once the HTTP request is complete. But the animation does not work. This is not a problem with the gif itself, since it works fine in all other usage. The problem seem to be connected to the fact, that the code is invoked from the Load
event (instead of a button click for example).
Can I add some code, to make the gif animate properly? Or is there a better event to use, than Load
?
Update
I did manage to change the code, so the animation starts. But quite quickly it freezes again. I did so using picLoading.Show()
and picLoading.Update()
, instead of the whole Visible
thing.
Load event (in user control)
private async void MyUserControl_Load(object sender, EventArgs e)
{
// Display the loading animated gif
picLoading.Visible = true;
// Wait for the http request to complete
await viewModel.HttpRequestMethod(host, path);
// Once completed, hide the picture again
picLoading.Visible = false;
}
HTTP request method (in view model)
public async Task<string> HttpRequestMethod(string host, string path)
{
HttpClient httpClient;
CookieContainer cookieContainer = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler()
{
CookieContainer = cookieContainer,
UseDefaultCredentials = true
};
httpClient = new HttpClient(handler)
{
BaseAddress = new Uri(host)
};
HttpResponseMessage httpResponse = httpClient.GetAsync(path).Result;
httpResponse.EnsureSuccessStatusCode();
return await httpResponse.Content.ReadAsStringAsync();
}