'How to run a gif on a window while waiting for a response from a server

I wrote a client on c#, and I want to run a gif on a loading form I made while waiting for a response from my server. Whenever I wait for a response from my server, the window just freezes until the message is received. I was wondering if there is a way to run my window (and the gif) while also waiting for a response.



Solution 1:[1]

Since you didn't provide implementation details, I can give you a crude example

public async Task LoadSomething()
{
    // Schedule the task on the thread pool and immediately return control.
    Task<MyResponse> serverCallTask = RunServerCallAsync();
    // Show the gif.
    RunGif();
    // Now we can await the result.
    await serverCallTask;
}

If you are not running your app on .NET core, you need to use await serverCallTask.ConfigureAwait(false); to avoid deadlock.

Asynchronous programming documentation: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/ Further explanation and avoiding deadlock: https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 funatparties