如何在等待服务器响应时在窗口上运行gif



我在c#上写了一个客户端,我想在等待服务器响应时在加载表单上运行一个gif。每当我等待服务器的响应时,窗口就会冻结,直到收到消息。我想知道是否有办法在等待响应的同时运行我的窗口(和gif(。

由于您没有提供实现细节,我可以给您一个粗略的示例

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;
}

如果你没有在.NET核心上运行你的应用程序,你需要使用await serverCallTask.ConfigureAwait(false);以避免死锁。

异步编程文档:https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/进一步解释并避免死锁:https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

最新更新