异步任务的结果被阻止

编程入门 行业动态 更新时间:2024-10-25 15:27:17
本文介绍了异步任务的结果被阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我尝试检索结果时,我遇到了任务阻塞的问题.

I have an issue with a task blocking when I try to retrieve it's result.

我有以下要同步执行的代码(这就是为什么我要寻找结果的原因)

I have the following piece of code I want executed synchronously (which is why I'm looking for the result)

我会忽略每次拨打电话的原因(传统软件需要通过不同的层进行多次通话)

I would ignore the reason each call has to be made (legacy software that requires multiple calls through different layers)

在启动要在PostCreateProfile中进行最终调用的任务之后,该调用似乎中断了,我可以看到此请求再也没有做到这一点了.

the call seems to break down after it starts the task for the final call to be made in the PostCreateProfile, I can see this request never makes it any further than this.

if (CreateProfile(demographics).Result) // Task blocks here { //dothing } private async Task<bool> CreateProfile(Demographics demographics) { ProfileService profileService = new ProfileService(); CreateProfileBindingModel createProfileBindingModel = this.CreateProfileModel(demographics); return await profileService.Create(createProfileBindingModel); } public async Task<bool> Create(CreateProfileBindingModel model) { HttpResponseMessage response = await profileServiceRequest.PostCreateProfile(rootURL, model); return response.IsSuccessStatusCode; } public Task<HttpResponseMessage> PostCreateProfile(string url, CreateProfileBindingModel model) { HttpContent contents = SerialiseModelData(model); var resultTask = client.PostAsync(url, contents); return resultTask; }

如果我将CreateProfile更改为异步void,则请求将到达其目的地,如下所示:

The request will reach its destination if I was to change CreateProfile to an async void like so:

private async void CreateProfile(AppointmentController controller) { ProfileService profileService = new ProfileService(); CreateProfileBindingModel createProfileBindingModel = this.CreateProfileModel(controller); await profileService.Create(createProfileBindingModel); }

但是我不能从中退回我想使用的布尔值. 谁能指出我做错了什么?

But I can't return the bool I want to use from this. Can anyone point out what I am doing wrong?

推荐答案

永远不要在异步/等待链上调用.Result .

任何调用CreateProfile(demographics)的代码都必须也是异步的,这样它才能做到

Whatever code that calls CreateProfile(demographics) needs to be async too so it can do

if (await CreateProfile(demographics)) { //dothing }

此外,如果可以的话,您真的应该将.ConfigureAwait(false)放在逻辑上可能的地方.

Also, if you can you really should put .ConfigureAwait(false) wherever it is logically possible.

if (await CreateProfile(demographics).ConfigureAwait(false)) // depending on what dothing is you may not want it here. { //dothing } private async Task<bool> CreateProfile(Demographics demographics) { ProfileService profileService = new ProfileService(); CreateProfileBindingModel createProfileBindingModel = this.CreateProfileModel(demographics); return await profileService.Create(createProfileBindingModel).ConfigureAwait(false); } public async Task<bool> Create(CreateProfileBindingModel model) { HttpResponseMessage response = await profileServiceRequest.PostCreateProfile(rootURL, model).ConfigureAwait(false); return response.IsSuccessStatusCode; } public Task<HttpResponseMessage> PostCreateProfile(string url, CreateProfileBindingModel model) { HttpContent contents = SerialiseModelData(model); var resultTask = client.PostAsync(url, contents); return resultTask; }

更多推荐

异步任务的结果被阻止

本文发布于:2023-11-15 00:35:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1589557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!