0

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();
}
Jakob Busk Sørensen
  • 5,599
  • 7
  • 44
  • 96
  • Take a look at [this post](https://stackoverflow.com/a/39142535/3110834). – Reza Aghaei Dec 08 '17 at 10:48
  • @RezaAghaei I have actually managed to make something like that, work. With a `Button_Click` event, that is. But when I use the `Load` event, then my gif refuse to animate correctly. – Jakob Busk Sørensen Dec 08 '17 at 10:50
  • 1
    Using the Load event is pretty dangerous, it can fire very early and the UI thread might not have been initialized sufficiently yet to allow async/await to work correctly. It doesn't do what it is supposed to do until Application.Run() is called. Set breakpoints. Consider that it needs help to decide when to run, add a public method and call it in the form's Shown event. – Hans Passant Dec 08 '17 at 11:37
  • @HansPassant I cannot see any `Shown` event. The event is occurring in a user control, inside a form. – Jakob Busk Sørensen Dec 08 '17 at 12:44
  • That's why I recommended to add a public method to your usercontrol. So the *form's* Shown event can help you get the download started. – Hans Passant Dec 08 '17 at 12:53
  • @HansPassant it's a bit more complicated, unfortunately. The user control is not shown until quite a bit later (based on user input). But I appreciate you input, and it is nice that you could confirm my suspicion regarding the `Load` event. – Jakob Busk Sørensen Dec 08 '17 at 13:06
  • @Noceo Load event of a `Control` is not a suitable place for showing such Loading window. – Reza Aghaei Dec 08 '17 at 14:05

0 Answers0