如何在WPF活动处理程序中使用async调用
我在WPF中使用异步调用的WPF中有这个活动处理程序。
private void Button_Click(object sender, RoutedEventArgs e)
{
var folderStructure = restApiProvider.GetFolderStructure();
}
这是getFolderStructure()
的实现:
public async Task<IList<string>> GetFolderStructure()
{
var request = new RestRequest(string.Empty, Method.Get);
var cancellationTokenSource = new CancellationTokenSource();
var response = await client.GetAsync(request, cancellationTokenSource.Token);
return JsonConvert.DeserializeObject<IList<string>>(response.Content);
}
我需要从getFolderStructure()
中获取数据以继续在应用程序中,但这是我获得的输出:
如果我添加foldersstructure.wait();
呼叫无法完成,并且整个应用程序都会被卡住。但是,如果我更改var foldersstructure = restapiprovider.getFolderStructure();
restapiprovider.getFolderStructure(); 呼叫立即完成并且固定不发生。
我想念什么?
I have this event handler in WPF with async call.
private void Button_Click(object sender, RoutedEventArgs e)
{
var folderStructure = restApiProvider.GetFolderStructure();
}
Here is the implementation of GetFolderStructure()
:
public async Task<IList<string>> GetFolderStructure()
{
var request = new RestRequest(string.Empty, Method.Get);
var cancellationTokenSource = new CancellationTokenSource();
var response = await client.GetAsync(request, cancellationTokenSource.Token);
return JsonConvert.DeserializeObject<IList<string>>(response.Content);
}
I need to get the data from GetFolderStructure()
in order to continue in the app but this is the output I get:
If I add folderStructure.Wait();
the call doesn't complete itself and the whole app get stuck. However if I change var folderStructure = restApiProvider.GetFolderStructure();
to restApiProvider.GetFolderStructure();
the call is completed immediately and stuck doesn't occure.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使调用方法异步并等待结果:
Make the calling method async and await the result: