c# - 如何在不冻结 Windows 窗体应用程序 C# WindowsPCL 的情况下获取客户端
问题描述
我正在尝试从我的网站获取发布数据以在 Windows 窗体应用程序中使用。当我尝试调用下面的方法来获取我网站的 WordPressClient 时,整个表单冻结并且没有响应。
public static async Task<WordPressClient> GetClient()
{
//JWT authentication
var client = new WordPressClient("http://website.com");
client.AuthMethod = AuthMethod.JWT;
//Form freezes on this line
await client.RequestJWToken("email", "password");
return client;
}
解决方案
我不确定那里发生的内部逻辑是什么,但尝试将WordPressClient
构造函数的调用包装成Task.Run
:
public static async Task<WordPressClient> GetClient()
{
try
{
WordPressClient client = null;
await Task.Run(() =>
{
//JWT authentication
client = new WordPressClient("http://website.com");
client.AuthMethod = AuthMethod.JWT;
});
//Form freezes on this line
await client.RequestJWToken("email", "password");
return client;
}
catch (Exception e) // this should be more specific Error Handler
{
//Console.WriteLine(e.Message);
throw;
}
}
另外,请确保正在运行的代码正在GetClient()
运行它async
:
var client = await GetClient();
推荐阅读
- python-3.x - shutil.move() 文件消失了
- javascript - 有没有办法从Javascript中的UTC时间提取时间字符串
- angularjs - 如何在 angularjs 中使用 ng-if 向下滑动和向上滑动元素?
- javascript - 输入 onChange 和 React 逻辑
- windows - 如何让共享文件夹与 Vagrant 和 Hyper-V 一起使用?
- android - 纵向使用recyclerview,横向使用表格布局
- python - 如何自动检测交通标志上的破坏行为?
- python - Python - 我们应该在 train_test_split() 中为 random_state 使用什么值以及在哪种情况下?
- .net-core - 如何使用 nginx 服务器在 .net core api 上启用 http/2?
- python - 如何遍历字符串并找到重复的值